Linked List is a data structure used for storing collection of data where successive elements are connected through pointers and the size of linked list can grow or shrink dynamically.
In short, Linked list can be thought as similar to array in which traversal can happen through pointers instead of indexes. Before going ahead have a look into Linked List Basics and Linked List Implementation.
Let’s have a look on basic class definition for Linked List.
class LinkedList
{
public:
int data;
LinkedList* next;
LinkedList (int data);
~LinkedList ();
};
/* Below function will print all nodes in a Linked List */
void print_linked_list (LinkedList* start)
{
if (!start)
{
cout << "Linked list is null, nothing to print" << endl;
return;
}
LinkedList* temp = start;
while (temp)
{
cout << " " << temp -> data;
temp = temp -> next;
}
cout << endl;
}
Swap nodes pairwise
Swapping pairwise in Linked List means position of a node is swapped to its next neighbor and this swapping keeps on happening in full Linked List in the same pairwise format.
Let’s look into the below figure to make it more clear.
As shown in the above image, Output Linked List nodes are swapped in a pair of nodes.
Algorithm to Swap nodes pairwise
- Start from the Head Node
- Take two nodes at a time and swap those.
- Repeat above steps with next pair of nodes till we reach the end of Linked List.
Swap nodes pairwise using Recursion
Let’s look into the sample code
LinkedList* pair_wise_swap_linked_list_recursion (LinkedList* start)
{
LinkedList* temp = NULL;
if (start && start -> next)
{
temp = start -> next;
start -> next = start -> next -> next;
temp -> next = start;
start = temp;
start -> next -> next = pair_wise_swap_linked_list_recursion (start -> next -> next);
}
return start;
}
Swap nodes pairwise using Iteration
Let’s look into the sample code.
LinkedList* pair_wise_swap_linked_list_iteration (LinkedList* start)
{
LinkedList* itr = start;
while (itr && itr -> next)
{
int data = itr -> data;
itr -> data = itr -> next -> data;
itr -> next -> data = data;
itr = itr -> next -> next;
}
return start;
}
Let’s define a main function to use above methods.
Few of the functions used below are explained in Linked List Insert operation, Linked List Delete Operation and Linked List Traverse and Search article . Refer those before moving ahead.
int main ()
{
LinkedList* start = NULL;
print_linked_list (start);
insert_at_begining (&start, 3);
insert_after_certain_element (start, 5, 6);
insert_after_certain_element (start, 7, 5);
insert_after_certain_element (start, 9, 8);
insert_after_certain_element (start, 11, 10);
insert_after_certain_element (start, 16, 11);
insert_after_certain_element (start, 2, 9);
insert_after_certain_element (start, 15, 2);
print_linked_list (start);
cout << "Swapping Pairwise Linked List Using Recursion" << endl;
start = pair_wise_swap_linked_list_recursion (start);
print_linked_list (start);
cout << "Swapping Pairwise Linked List Using Iteration" << endl;
start = pair_wise_swap_linked_list_iteration (start);
print_linked_list (start);
cout << "Deleting Head Node " << endl;
delete_start_node (&start);
print_linked_list (start);
cout << "Deleting Node 11" << endl;
delete_searched_node (&start, 11);
print_linked_list (start);
cout << "Deleting all Nodes" << endl;
delete_all_node (&start);
print_linked_list (start);
}
Let’s analyze the output of this main function.
Linked list is null, nothing to print
Inserting node 5
Searched data not found, hence putting new node at the end
Inserting node 7
Inserting node 9
Searched data not found, hence putting new node at the end
Inserting node 11
Searched data not found, hence putting new node at the end
Inserting node 16
Inserting node 2
Inserting node 15
3 5 7 9 2 15 11 16
Swapping Pairwise Linked List Using Recursion
5 3 9 7 15 2 16 11
Swapping Pairwise Linked List Using Iteration
3 5 7 9 2 15 11 16
Deleting Head Node
5 7 9 2 15 11 16
Deleting Node 11
Node found, going to delete
5 7 9 2 15 16
Deleting all Nodes
Linked list is null, nothing to print