Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5 then the function should change it to 2->1->4->3->5, and if the linked list is 1->2->3->4->5->6 then the function should change it to 2->1->4->3->6->5.
METHOD 1 (Iterative)
Start from the head node and traverse the list. While traversing swap data of each node with its next node’s data
Start from the head node and traverse the list. While traversing swap data of each node with its next node’s data
C++ Programming:
[pastacode lang=”cpp” manual=”%2F*%20C%20program%20to%20pairwise%20swap%20elements%20in%20a%20given%20linked%20list%20*%2F%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20linked%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%2F*Function%20to%20swap%20two%20integers%20at%20addresses%20a%20and%20b%20*%2F%0Avoid%20swap(int%20*a%2C%20int%20*b)%3B%0A%20%0A%2F*%20Function%20to%20pairwise%20swap%20elements%20of%20a%20linked%20list%20*%2F%0Avoid%20pairWiseSwap(struct%20node%20*head)%0A%7B%0A%20%20%20%20struct%20node%20*temp%20%3D%20head%3B%0A%20%0A%20%20%20%20%2F*%20Traverse%20further%20only%20if%20there%20are%20at-least%20two%20nodes%20left%20*%2F%0A%20%20%20%20while%20(temp%20!%3D%20NULL%20%26%26%20temp-%3Enext%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20Swap%20data%20of%20node%20with%20its%20next%20node’s%20data%20*%2F%0A%20%20%20%20%20%20%20%20swap(%26temp-%3Edata%2C%20%26temp-%3Enext-%3Edata)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Move%20temp%20by%202%20for%20the%20next%20pair%20*%2F%0A%20%20%20%20%20%20%20%20temp%20%3D%20temp-%3Enext-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20*%2F%0A%2F*%20Function%20to%20swap%20two%20integers%20*%2F%0Avoid%20swap(int%20*a%2C%20int%20*b)%0A%7B%0A%20%20%20%20int%20temp%3B%0A%20%20%20%20temp%20%3D%20*a%3B%0A%20%20%20%20*a%20%3D%20*b%3B%0A%20%20%20%20*b%20%3D%20temp%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20add%20a%20node%20at%20the%20begining%20of%20Linked%20List%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20linked%20list%20*%2F%0Avoid%20printList(struct%20node%20*node)%0A%7B%0A%20%20%20%20while%20(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20node-%3Edata)%3B%0A%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20struct%20node%20*start%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20The%20constructed%20linked%20list%20is%3A%0A%20%20%20%20%201-%3E2-%3E3-%3E4-%3E5%20*%2F%0A%20%20%20%20push(%26start%2C%205)%3B%0A%20%20%20%20push(%26start%2C%204)%3B%0A%20%20%20%20push(%26start%2C%203)%3B%0A%20%20%20%20push(%26start%2C%202)%3B%0A%20%20%20%20push(%26start%2C%201)%3B%0A%20%0A%20%20%20%20printf(%22Linked%20list%20before%20calling%20%20pairWiseSwap()%5Cn%22)%3B%0A%20%20%20%20printList(start)%3B%0A%20%0A%20%20%20%20pairWiseSwap(start)%3B%0A%20%0A%20%20%20%20printf(%22%5CnLinked%20list%20after%20calling%20%20pairWiseSwap()%5Cn%22)%3B%0A%20%20%20%20printList(start)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]
Output:
Linked List before calling pairWiseSwap() 1 2 3 4 5 Linked List after calling pairWiseSwap() 2 1 4 3 5
Time complexity: O(n)
METHOD 2 (Recursive)
If there are 2 or more than 2 nodes in Linked List then swap the first two nodes and recursively call for rest of the list.
C++ Programming:
[pastacode lang=”cpp” manual=”%2F*%20Recursive%20function%20to%20pairwise%20swap%20elements%20of%20a%20linked%20list%20*%2F%0Avoid%20pairWiseSwap(struct%20node%20*head)%0A%7B%0A%20%20%2F*%20There%20must%20be%20at-least%20two%20nodes%20in%20the%20list%20*%2F%0A%20%20if%20(head%20!%3D%20NULL%20%26%26%20head-%3Enext%20!%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20%20%20%2F*%20Swap%20the%20node’s%20data%20with%20data%20of%20next%20node%20*%2F%0A%20%20%20%20%20%20swap(%26head-%3Edata%2C%20%26head-%3Enext-%3Edata)%3B%0A%20%20%20%20%0A%20%20%20%20%20%20%2F*%20Call%20pairWiseSwap()%20for%20rest%20of%20the%20list%20*%2F%0A%20%20%20%20%20%20pairWiseSwap(head-%3Enext-%3Enext)%3B%0A%20%20%7D%20%20%0A%7D” message=”” highlight=”” provider=”manual”/]
Time complexity: O(n)