Pairwise swap adjacent nodes of a linked list by changing pointers | Set 2
Given a singly linked list, write a function to swap elements pairwise.
Input : 1->2->3->4->5->6->7 Output : 2->1->4->3->6->5->7, Input : 1->2->3->4->5->6 Output : 2->1->4->3->6->5
A solution has been discussed set 1. Here a simpler solution is discussed. We explicitly change pointers of first two nodes, then fix remaining nodes.
C++
/* This program swaps the nodes of linked list rather than swapping the field from the nodes. Imagine a case where a node contains many fields, there will be plenty of unnecessary swap calls. */#include<bits/stdc++.h> using namespace std; /* A linked list node */struct Node { int data; struct Node *next; }; /* Function to pairwise swap elements of a linked list */Node *pairWiseSwap(Node *head) { // If linked list is empty or there is only // one node in list if (head == NULL || head->next == NULL) return head; // Fix the head and its next explicitly to // avoid many if else in while loop Node *curr = head->next->next; Node *prev = head; head = head->next; head->next = prev; // Fix remaining nodes while (curr != NULL && curr->next != NULL) { prev->next = curr->next; prev = curr; Node *next = curr->next->next; curr->next->next = curr; curr = next; } prev->next = curr; return head; } /* Function to add a node at the beginning of Linked List */void push(struct Node** head_ref, int new_data) { struct Node* new_node = new Node; new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } /* Function to print nodes in a given linked list */void printList(struct Node *node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } /* Druver program to test above function */int main() { struct Node *start = NULL; /* The constructed linked list is: 1->2->3->4->5->6->7 */ push(&start;, 7); push(&start;, 6); push(&start;, 5); push(&start;, 4); push(&start;, 3); push(&start;, 2); push(&start;, 1); printf("\n Linked list before calling pairWiseSwap() "); printList(start); start = pairWiseSwap(start); printf("\n Linked list after calling pairWiseSwap() "); printList(start); return 0; } |
chevron_right
filter_none
Java
/* This program swaps the nodes of linked list rather than swapping the field from the nodes. Imagine a case where a node contains many fields, there will be plenty of unnecessary swap calls. */class GfG { /* A linked list node */static class Node { int data; Node next; } static Node head = null; /* Function to pairwise swap elements of a linked list */static Node pairWiseSwap(Node head) { // If linked list is empty or there is only // one node in list if (head == null || head.next == null) return head; // Fix the head and its next explicitly to // avoid many if else in while loop Node curr = head.next.next; Node prev = head; head = head.next; head.next = prev; // Fix remaining nodes while (curr != null && curr.next != null) { prev.next = curr.next; prev = curr; Node next = curr.next.next; curr.next.next = curr; curr = next; } prev.next = curr; return head; } /* Function to add a node at the beginning of Linked List */static void push(int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = (head); (head) = new_node; } /* Function to print nodes in a given linked list */static void printList(Node node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } } /* Driver code */public static void main(String[] args) { //Node head = null; /* The constructed linked list is: 1->2->3->4->5->6->7 */ push( 7); push( 6); push( 5); push( 4); push(3); push( 2); push( 1); System.out.print("\n Linked list before calling pairWiseSwap() "); printList(head); Node start = pairWiseSwap(head); System.out.print("\n Linked list after calling pairWiseSwap() "); printList(start); } } // This code is contributed by Prerna Saini. |
chevron_right
filter_none
C#
/* This program swaps the nodes of linked list rather than swapping the field from the nodes. Imagine a case where a node contains many fields, there will be plenty of unnecessary swap calls. */using System; class GfG { /* A linked list node */class Node { public int data; public Node next; } static Node head = null; /* Function to pairwise swap elements of a linked list */static Node pairWiseSwap(Node head) { // If linked list is empty or there // is only one node in list if (head == null || head.next == null) return head; // Fix the head and its next explicitly to // avoid many if else in while loop Node curr = head.next.next; Node prev = head; head = head.next; head.next = prev; // Fix remaining nodes while (curr != null && curr.next != null) { prev.next = curr.next; prev = curr; Node next = curr.next.next; curr.next.next = curr; curr = next; } prev.next = curr; return head; } /* Function to add a node at the beginning of Linked List */static void push(int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = (head); (head) = new_node; } /* Function to print nodes in a given linked list */static void printList(Node node) { while (node != null) { Console.Write(node.data + " "); node = node.next; } } /* Driver code */public static void Main() { //Node head = null; /* The constructed linked list is: 1->2->3->4->5->6->7 */ push( 7); push( 6); push( 5); push( 4); push(3); push( 2); push( 1); Console.Write("\n Linked list before" + "calling pairWiseSwap() "); printList(head); Node start = pairWiseSwap(head); Console.Write("\n Linked list after" + "calling pairWiseSwap() "); printList(start); } } // This code is contributed by PrinciRaj1992 |
chevron_right
filter_none
Output:
Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7 Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7
Recommended Posts:
- Find the middle of a given linked list in C and Java
- Program for n'th node from the end of a Linked List
- Write a function to get Nth node in a Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Detect loop in a linked list
- Write a function to delete a Linked List
- Write a function that counts the number of times a given int occurs in a Linked List
- Reverse a linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Write a function to get the intersection point of two Linked Lists
- Function to check if a singly linked list is palindrome
- The Great Tree-List Recursion Problem.
- Clone a linked list with next and random pointer | Set 1
- Memory efficient doubly linked list
- Given a linked list which is sorted, how will you insert in sorted way
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



