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 in C++

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 arbit pointer of the duplicate of the current->arbit node to point to that node's duplicate (such that mp[x]->arbit points 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:

Clone a Linked List with next and Random Pointer in C++
  • The arbit points are the green links.

Making a duplicate of Nodes and the next pointer:

  • Create a single linked list of duplicate nodes at first, mapping the new nodes to the old ones using only the next pointers.
  • Here, the mapping is displayed via the blue links.
Clone a Linked List with next and Random Pointer in C++

Linking the arbit pointers:

As stated in the technique, iterate the old array now and change the arbit pointers. The arbit points are the green links.

At first node:

Clone a Linked List with next and Random Pointer in C++

At second node:

Clone a Linked List with next and Random Pointer in C++

At Third node:

Clone a Linked List with next and Random Pointer in C++

At fourth node:

Clone a Linked List with next and Random Pointer in C++

At fifth node:

Clone a Linked List with next and Random Pointer in C++

The final linked list is as shown below:

Clone a Linked List with next and Random Pointer in C++

Program:

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

Output:

Clone a Linked List with next and Random Pointer in C++