C++ Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links
Last Updated :
13 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
class node
{
public :
int data;
node* next;
};
node* pairWiseSwap(node* head)
{
if (head == NULL ||
head->next == NULL)
return head;
node* prev = head;
node* curr = head->next;
head = curr;
while ( true )
{
node* next = curr->next;
curr->next = prev;
if (next == NULL ||
next->next == NULL)
{
prev->next = next;
break ;
}
prev->next = next->next;
prev = next;
curr = prev->next;
}
return head;
}
void push(node** head_ref,
int new_data)
{
node* new_node = new node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(node* node)
{
while (node != NULL)
{
cout << node->data << " " ;
node = node->next;
}
}
int main()
{
node* start = NULL;
push(&start, 7);
push(&start, 6);
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
cout << "Linked list before " <<
"calling pairWiseSwap() " ;
printList(start);
start = pairWiseSwap(start);
cout << "Linked list after calling" <<
"pairWiseSwap() " ;
printList(start);
return 0;
}
|
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
Time 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.
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
class node
{
public :
int data;
node* next;
};
node* pairWiseSwap(node* head)
{
if (head == NULL ||
head->next == NULL)
return head;
node* remaining = head->next->next;
node* newhead = head->next;
head->next->next = head;
head->next = pairWiseSwap(remaining);
return newhead;
}
void push(node** head_ref, int new_data)
{
node* new_node = new node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(node* node)
{
while (node != NULL) {
cout << node->data << " " ;
node = node->next;
}
}
int main()
{
node* start = NULL;
push(&start, 7);
push(&start, 6);
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
cout <<
"Linked list before calling pairWiseSwap() " ;
printList(start);
start = pairWiseSwap(start);
cout <<
"Linked list after calling pairWiseSwap() " ;
printList(start);
return 0;
}
|
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
Time Complexity: O(n)
Auxiliary Space: O(n)
Please refer complete article on Pairwise swap elements of a given linked list by changing links for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...