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 then the function should change it to. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. METHOD 1 (Iterative) Below is the implementation of the above approach: C++
C
Java
Python
C#
Javascript
temp.next.data = k; temp = temp.next.next; } } /* Utility functions */ /* Inserts a new Node at front of the list. */ function push(new_data) { /* * 1 & 2: Allocate the Node & Put in the data */ var new_node = new Node(new_data); /* 3. Make next of new Node as head */ new_node.next = head; /* 4. Move the head to point to new Node */ head = new_node; } /* Function to print linked list */ function printList() { var temp = head; while (temp != null) { document.write(temp.data + " "); temp = temp.next; } document.write("<br/>"); } /* Driver program to test above functions */ /* Created Linked List 1->2->3->4->5 */ push(5); push(4); push(3); push(2); push(1); document.write( "Linked List before calling pairWiseSwap() <br/>" ); printList(); pairWiseSwap(); document.write( "Linked List after calling pairWiseSwap()<br/> " ); printList(); // This code is contributed by todaysgaurav </script> 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) Below image is a dry run of the above approach: Below is the implementation of the above approach: C
Java
Python3
C#
Javascript
Time complexity: O(n) The solution provided there swaps data of nodes. If data contains many fields, there will be many swap operations. See this for an implementation that changes links rather than swapping data. Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem. Article Tags : Linked List Amazon Microsoft Moonfrog Labs Practice Tags : Moonfrog Labs Amazon Microsoft Linked List Read Full Article Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7, 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 This problem has been discussed here. The solution provided there swaps data of nodes. If data contains many fields, there will be many swap operations. So changing links is a better idea in general. Following is the implementation that changes links instead of swapping data. C++
C
Java
static class Node { int data; Node next; Node(int d) { data = d; next = null; } } /* Function to pairwise swap elements of a linked list */ Node pairWiseSwap(Node node) { // If linked list is empty or there is only one node in list if (node == null || node.next == null) { return node; } // Initialize previous and current pointers Node prev = node; Node curr = node.next; node = curr; // Change head before proceeding // Traverse the list while (true) { Node next = curr.next; curr.next = prev; // Change next of current as previous node // If next NULL or next is the last node if (next == null || next.next == null) { prev.next = next; break; } // Change next of previous to next next prev.next = next.next; // Update previous and curr prev = next; curr = prev.next; } return node; } /* Function to print nodes in a given linked list */ void printList(Node node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } } // Driver program to test above functions public static void main(String[] args) { /* The constructed linked list is: 1->2->3->4->5->6->7 */ LinkedList list = new LinkedList(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); list.head.next.next.next = new Node(4); list.head.next.next.next.next = new Node(5); list.head.next.next.next.next.next = new Node(6); list.head.next.next.next.next.next.next = new Node(7); System.out.println("Linked list before calling pairwiseSwap() "); list.printList(head); Node st = list.pairWiseSwap(head); System.out.println(""); System.out.println("Linked list after calling pairwiseSwap() "); list.printList(st); System.out.println(""); } } // This code has been contributed by Mayank Jaiswal Python3
C#
Javascript
Output: Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7 Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7Time Complexity: The time complexity of the above program is O(n) where n is the number of nodes in a given linked list. The while loop does a traversal of the given linked list. Following is the recursive implementation of the same approach. We change the first two nodes and recur for the remaining list. Thanks to geek and omer salem for suggesting this method. C++
C
Java
Python3
C#
Javascript
Output: Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7 Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7Pairwise swap adjacent nodes of a linked list by changing pointers | Set 2 Article Tags : Linked List Linked Lists Practice Tags : Linked List Read Full Article In this program, we need to swap given two nodes in the singly linked list without swapping data. One of the approaches to accomplish this task is to swap the previous nodes of the given two nodes and then, swap the next nodes of two nodes. Algorithm
SolutionPythonOutput: Original list: 1 2 3 4 5 List after swapping nodes: 1 5 3 4 2 COutput: Original list: 1 2 3 4 5 List after swapping nodes: 1 5 3 4 2 JAVAOutput: Original list: 1 2 3 4 5 List after swapping nodes: 1 5 3 4 2 C#Output: Original list: 1 2 3 4 5 List after swapping nodes: 1 5 3 4 2 PHPOutput: Original list: 1 2 3 4 5 List after swapping nodes: 1 5 3 4 2 |