A Linked List is a linear data structure in which each node is having two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.
Let us assume that we have a linked list such that each node contains a random pointer which is pointing to other nodes in the list. The task is to construct the list with the same as the original list. Copying the list from the original list which is having some random pointer is called a 'Deep Copy' of the linked list.
For Example
Input-1
Output:
5-> 2 -> 3 -> 7 ->4 ->
Explanation: If we append the new list with the value of the original nodes in the given linked list and replace the random pointer of the original linked list with the next node in the new list, then it will become 5-> 2- >3 -> 7-> 4->
We have a linked list with nodes containing its data and a random pointer. To achieve the copy of the linked list with the data and random pointer, we will first append the new node with the same value after each node. This will create a duplicate node after each node.
After initialization, check the path of the random pointer in the list and place the random pointer accordingly into the newly created node.
Now separating the newly created nodes after each node in the original list will create a deep copy of the Linked List.
#include <bits/stdc++.h> using namespace std; struct listnode { int data; listnode * next, * random; listnode(int d) { data = d; next = NULL; random = NULL; } }; void print(listnode * head) { listnode * curr = head; while (curr) { cout << curr -> data << " " << curr -> random -> data << endl; curr = curr -> next; } } listnode * copyRandomList(listnode * head) { if (!head) { return head; } //Insert a new node with the same value after each node in the original list. listnode * curr = head; while (curr) { listnode * newHead = new listnode(curr -> data); newHead -> next = curr -> next; curr -> next = newHead; curr = curr -> next -> next; } //Now place the randompointer with the newly created node. curr = head; while (curr) { if (curr -> random) (curr -> next) -> random = (curr -> random) -> next; curr = curr -> next -> next; } //Now Let us separate the newly created list curr = head; listnode * result = curr -> next; listnode * dummyHead = new listnode(-1); dummyHead -> next = result; while (curr) { curr -> next = result -> next; curr = curr -> next; if (curr) { result -> next = curr -> next; } result = result -> next; } result = dummyHead -> next; delete dummyHead; return result; } int main() { listnode * head = new listnode(5); head -> next = new listnode(6); head -> next -> next = new listnode(3); head -> next -> next -> next = new listnode(4); head -> next -> next -> next -> next = new listnode(2); head -> random = head -> next -> next; head -> next -> random = head; head -> next -> next -> random = head -> next -> next -> next -> next; head -> next -> next -> next -> random = head -> next -> next -> next -> next; head -> next -> next -> next -> next -> random = head -> next; cout << "Original list :" << endl; print(head); cout << "Deep Copy of the List:" << endl; listnode * deep_copyList = copyRandomList(head); print(deep_copyList); return 0; }
Running the above code will generate the output as,
Original List: 5 3 6 5 3 2 4 2 2 6 Deep Copy of the List: 5 3 6 5 3 2 4 2 2 6