Clone A Linked List With Next And Random Pointer In C++

In this article, you will learn about how to clone a linked list with next and Random Pointer in C++.

  • Each node in a linked list of size N has two connections: one points to the node immediately following it, and the other can point to any other node in the list. This linked list must be duplicated in O(N) time.
  • Keep in mind that the "arbit" pointer may randomly go to any node in the linked list, whereas the "next" pointer points to the node that follows in the linked list.
  • The image below displays a linked list example:
  • Clone a Linked List with next and Random Pointer using Extra Space:

  • In order to map the new nodes to their matching nodes in the current linked list, a single linked list consisting solely of the 'next' pointer must first be created. With this mapping, you may use any node in the newly created list to point to any other node.

To apply the aforementioned idea, take the actions indicated below:

  • Replicate each node (let's say X), let's say Y, and then map each node to its corresponding old node (let's say mp) so that mp[X] = Y.
  • With only the 'next' reference for each node, it creates a single linked list of duplicate nodes.

For the preceding linked list iterations, follow these procedures again:

  • Identify the redundant node that is linked to the primary one. (For instance, if X is the current node, then mp[x] is the duplicate.)
  • Set the arbicpp tutorialer of the duplicate of the current->arbit node to point to that node's duplicate (such that mp[x]->arbicpp tutorials to mp[X->arbit]).

The required linked list, which is a linked list, is created using this procedure.

For clarification, refer to the figure below:

Illustration:

Consider the linked list shown below:

  • The arbicpp tutorials are the green links.
  • Making a duplicate of Nodes and the nexcpp tutorialer:

  • Create a single linked list of duplicate nodes at first, mapping the new nodes to the old ones using only the nexcpp tutorialers.
  • Here, the mapping is displayed via the blue links.
  • Linking the arbicpp tutorialers:

As stated in the technique, iterate the old array now and change the arbicpp tutorialers. The arbicpp tutorials are the green links.

At first node:

At second node:

At Third node:

At fourth node:

At fifth node:

The final linked list is as shown below:

Program:

Let us take an example to demonstrate how to clone a linked list with next and random pointer in C++.

Example

#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
Node* next;
Node* arbit;
Node(int x)
{
this->val = x;
this->next = NULL;
this->arbit = NULL;
}
};
Node* cloneLinkedList(Node* head)
{
unordered_map<Node*, Node*> mp;
Node *temp, *nhead;
temp = head;
nhead = new Node(temp->val);
mp[temp] = nhead;
while (temp->next != NULL) {
nhead->next = new Node(temp->next->val);
temp = temp->next;
nhead = nhead->next;
mp[temp] = nhead;
}
temp = head;
while (temp != NULL) {
mp[temp]->arbit = mp[temp->arbit];
temp = temp->next;
}
return mp[head];
}
void printList(Node* head)
{
cout << head->val << "("<< head->arbit->val << ")";
head = head->next;
while (head != NULL) {
cout << " -> " << head->val << "("<< head->arbit->val << ")";
head = head->next;
}
cout << endl;
}
int main()
{
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
head->arbit = head->next->next;
head->next->arbit = head;
head->next->next->arbit = head->next->next->next->next;
head->next->next->next->arbit = head->next->next;
head->next->next->next->next->arbit = head->next;
cout << "The original linked list:\n";
printList(head);
Node* sol = cloneLinkedList(head);
cout << "The cloned linked list:\n";
printList(sol);
return 0;
}

Output:

Input Required

This code uses input(). Please provide values below: