# 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 -> 67After swapping: LINKED LIST:13 -> 12 -> 21 -> 15 -> 67Other example: LINKED LIST:12 -> 13 -> 15 -> 21After swapping: LINKED LIST:13 -> 12 -> 21 -> 15

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

- If linked list is empty or contains only one node, then return.
- Traverse the linked list and swap each node’s data with the next node’s data, if exist.
- 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);
}
```

Printing the Linked List before Swapping: 12 34 88 56 66 78 Printing the Linked List after Swapping: 34 12 56 88 78 66OUTPUT:

**Related Posts:**

**Count All Occurrences of a Particular Node in a Linked List.****Bubble Sort on Linked List.****Detect a Loop in a Linked List.****Find the Length of the Loop present in the Linked List.****Detect and Remove Loop from a Linked List.****Segregate Even and Odd Nodes of the Linked List.****Delete Complete Linked List.****Delete Nth Node of the Linked List.****Delete without head pointer of the Linked List.****Delete All Occurrences of particular node of the Linked List.****Delete Alternate Nodes of the Linked List.****Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.****Remove Duplicate Nodes from an Unsorted Linked List.****Remove Duplicate Nodes from a Sorted Linked List.****Find Union and Intersection of Two Linked List.****Merge Two Sorted Linked List.****Insert a New Node at the Sorted Linked List.****Reverse a Linked List.****Reverse a Linked List Using Stack.****Printing Linked List in Reverse Order without actually reversing the Linked List.**