


We have a linked list 1->2->3->4->5->6->7 and so on. You are supposed to swap pairs of a linked list like swap (1,2), (3,4), (5,6), and so on.
1. You may not modify the data in the list’s nodes; only nodes themselves may be changed. Because imagine a case where a node contains many fields, so there will be too much unnecessary swap.
2. If a pair of a node does not exist, then leave the node as it is.
The input contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
For each input, print a single line containing the same number of integers as in the list, in swapped order.
You do not need to print anything, it has already been taken care of just implement the given function.
0 <= N <= 5 * 10 ^ 5
-10 ^ 9 <= DATA <= 10 ^ 9 and DATA != -1
Where ‘N’ is the length of the linked list and 'DATA' is data in each node.
Time limit: 1 sec.
We traverse the linked list using recursion and consider two nodes at a time and swap their links and pass the next pair information to recursion.
'FIRST' = 'HEAD'
'SECOND' = 'HEAD' -> 'NEXT' 'REMAINING-NODE' = 'SECOND' -> 'NEXT' 'NEW-HEAD' = 'SECOND' 'SECOND' -> 'NEXT' = 'FIRST'Our idea is to swap the links of each pair from the “HEAD” of the list until we reach the end of the list or there is only one element left.
Here, note that after the swap, the head of our list may change if the size of the input list is larger than one, so we are using a dummy node as a placeholder to splice our result list. In this way, we can start with the dummy node and check if there exists a pair of nodes after the current pointer we perform a swap on their links.
Steps:
'DUMMY' -> 'NEXT'='HEAD' 'CURR' = 'DUMMY';Maximum Island Size in a Binary Tree
Equal Subtree Sums
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators