Swap elements of adjacent nodes of the Linked List

Swap elements of adjacent nodes of the Linked List” is one of the basic programming problem based on Linked List data structure. Here, we are given a linked list and our task is to swap elements of adjacent nodes of the linked list.

Example:

Suppose, we need to swap elements of adjacent nodes of the following linked list:
LINKED LIST: 
12 -> 13 -> 15 -> 21 -> 67
After swapping: 
LINKED LIST:       
13 -> 12 -> 21 -> 15 -> 67
Other example: 
LINKED LIST:       
12 -> 13 -> 15 -> 21 
After swapping: 
LINKED LIST:       
13 -> 12 -> 21 -> 15 

The steps required to swap elements of adjacent nodes of the linked list is as follows:

  1. If linked list is empty or contains only one node, then return.
  2. Traverse the linked list and swap each node’s data with the next node’s data, if exist.
  3. Increment the pointer by two nodes while traversing.

C++ Program to swap elements of adjacent nodes of the linked list is as follows:

/* C++ Program to swap elements of adjacent nodes of the linked list */
#include<bits/stdc++.h>
using namespace std;

/* Strcuture of the Node of the Linked List */
typedef struct node
{
    int data;
    struct node *next;
}Node;

/* Function to Insert Node at the end of the Linked List */
void insert_end(Node **head, int ele)
{
    /* Creating a new node */
    Node *new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = ele;
    new_node->next = NULL;
    
    /* check whether the linked list is empty or not */
    
    /* If Linked List is Empty, then new node will become head of the linked list */
    if(*head == NULL)     
    {
        *head = new_node;
        return;
    }
    
    /* If Linked List is not Empty, then traverse the complete linked list and point 
    the pointer of last node of the linked list to the new node of the linked list */
    Node *temp = NULL;
    temp = (*head);
    while(temp->next != NULL)
    temp=temp->next;
    temp->next = new_node;
}

/* Function to pairwise swap elements of a linked list */
void swapAdjacent(Node *head)
{
    /* If Linked List is empty or contains only one node, then return. */
    if(head == NULL || head->next == NULL)
    return;
    
    /* If Linked List contains more than one nodes, then traverse the 
       linked list and swap each node’s data with the next node’s data, if exist.
    */
    Node *temp = head;
    while(temp && temp->next)
    {
        int temp_var = temp->data;
        temp->data = temp->next->data;
        temp->next->data = temp_var;
        
        temp = temp -> next -> next;
    }    
}

/* Function to Print the Linked List */
void print(Node *head)
{
    Node *temp = head;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<endl;
}
int main()
{
    Node *head = NULL;
    /* Inserting Some Nodes in the Linked List */
    insert_end(&head,12);
    insert_end(&head,34);
    insert_end(&head,88);
    insert_end(&head,56);
    insert_end(&head,66);
    insert_end(&head,78);
    cout<<"Printing the Linked List before Swapping:\n";
    print(head);
    
    /* swap adjacent node data */
    swapAdjacent(head);
    
    cout<<"\nPrinting the Linked List after Swapping:\n";
    print(head);
    
}
OUTPUT:
Printing the Linked List before Swapping:
12 34 88 56 66 78
Printing the Linked List after Swapping:
34 12 56 88 78 66

Related Posts:

  1. Count All Occurrences of a Particular Node in a Linked List.
  2. Bubble Sort on Linked List.
  3. Detect a Loop in a Linked List.
  4. Find the Length of the Loop present in the Linked List.
  5. Detect and Remove Loop from a Linked List.
  6. Segregate Even and Odd Nodes of the Linked List.
  7. Delete Complete Linked List.
  8. Delete Nth Node of the Linked List.
  9. Delete without head pointer of the Linked List.
  10. Delete All Occurrences of particular node of the Linked List.
  11. Delete Alternate Nodes of the Linked List.
  12. Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
  13. Remove Duplicate Nodes from an Unsorted Linked List.
  14. Remove Duplicate Nodes from a Sorted Linked List.
  15. Find Union and Intersection of Two Linked List.
  16. Merge Two Sorted Linked List.
  17. Insert a New Node at the Sorted Linked List.
  18. Reverse a Linked List.
  19. Reverse a Linked List Using Stack.
  20. Printing Linked List in Reverse Order without actually reversing the Linked List.

You may also like...