# Clone a linked list with next and random pointer | Set 2

We have already discussed 2 different ways to clone a linked list. In this post, one more simple method to clone a linked list is discussed.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

The idea is to use Hashing. Below is algorithm.
1. Traverse the original linked list and make a copy in terms of data.
2. Make a hash map of key value pair with original linked list node and copied linked list node.
3. Traverse the original linked list again and using the hash map adjust the next and random reference of cloned linked list nodes.

Below is the implementation of above approach.

## C++

```// C++ program to clone a linked list with
// random pointers
#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;//Node data

// Next and random reference
Node *next, *random;

Node(int data)
{
this->data = data;
this->next = this->random = NULL;
}
};

{
public:

{
}

// push method to put data always at
void push(int data)
{
Node *node = new Node(data);
}

// Method to print the list.
void print()
{
while (temp != NULL)
{
Node *random = temp->random;
int randomData = (random != NULL)?
random->data: -1;
cout << "Data = " << temp->data
<< ", ";
cout << "Random Data = " <<
randomData << endl;
temp = temp->next;
}
cout << endl;
}

// Actual clone method which returns
// list.
{
// Initialize two references,
// one with original list's head.
Node *cloneCurr = NULL;

// Hash map which contains node
// to node mapping of original
unordered_map<Node*, Node*> mymap;

// Traverse the original list and
// make a copy of that in the
while (origCurr != NULL)
{
cloneCurr = new Node(origCurr->data);
mymap[origCurr] = cloneCurr;
origCurr = origCurr->next;
}

// reference again.

// Traversal of original list again
// to adjust the next and random
// references of clone list using
// hash map.
while (origCurr != NULL)
{
cloneCurr = mymap[origCurr];
cloneCurr->next = mymap[origCurr->next];
cloneCurr->random = mymap[origCurr->random];
origCurr = origCurr->next;
}

// return the head reference of
// the clone list.
}
};

// driver code
int main()
{
// Pushing data in the linked list.
mylist->push(4);
mylist->push(3);
mylist->push(2);
mylist->push(1);

// Setting up random references.

// Making a clone of the original

// Print the original and cloned
mylist->print();
clone->print();
}
// This code is contributed by Chhavi
```

## Java

```// Java program to clone a linked list with random pointers
import java.util.HashMap;
import java.util.Map;

class Node
{
int data;//Node data
Node next, random;//Next and random reference

//Node constructor
public Node(int data)
{
this.data = data;
this.next = this.random = null;
}
}

{

{
}

// push method to put data always at the head
public void push(int data)
{
Node node = new Node(data);
}

// Method to print the list.
void print()
{
while (temp != null)
{
Node random = temp.random;
int randomData = (random != null)? random.data: -1;
System.out.println("Data = " + temp.data +
", Random data = "+ randomData);
temp = temp.next;
}
}

// Actual clone method which returns head
// reference of cloned linked list.
{
// Initialize two references, one with original
Node origCurr = this.head, cloneCurr = null;

// Hash map which contains node to node mapping of
// original and clone linked list.
Map<Node, Node> map = new HashMap<Node, Node>();

// Traverse the original list and make a copy of that
// in the clone linked list.
while (origCurr != null)
{
cloneCurr = new Node(origCurr.data);
map.put(origCurr, cloneCurr);
origCurr = origCurr.next;
}

// Adjusting the original list reference again.

// Traversal of original list again to adjust the next
// and random references of clone list using hash map.
while (origCurr != null)
{
cloneCurr = map.get(origCurr);
cloneCurr.next = map.get(origCurr.next);
cloneCurr.random = map.get(origCurr.random);
origCurr = origCurr.next;
}

//return the head reference of the clone list.
}
}

// Driver Class
class Main
{
// Main method.
public static void main(String[] args)
{
// Pushing data in the linked list.
list.push(4);
list.push(3);
list.push(2);
list.push(1);

// Setting up random references.

// Making a clone of the original linked list.

// Print the original and cloned linked list.
list.print();
clone.print();
}
}```

Output:
```Original linked list
Data = 1, Random data = 3
Data = 2, Random data = 4
Data = 3, Random data = 5
Data = 4, Random data = -1
Data = 5, Random data = 2

Data = 1, Random data = 3
Data = 2, Random data = 4
Data = 3, Random data = 5
Data = 4, Random data = -1
Data = 5, Random data = 2```

Time complexity : O(n)
Auxiliary space : O(n)

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.4 Average Difficulty : 3.4/5.0
Based on 93 vote(s)