Clone a Linked List with random pointers

Easy
0/40
75 upvotes
Asked in companies
QualcommAmazonThales

Problem statement

You are given a linked list containing 'n' nodes, where every node in the linked list contains two pointers:


(1) ‘next’ which points to the next node in the list

(2) ‘random’ which points to a random node in the list or 'null'.


Your task is to create a 'deep copy' of the given linked list and return its head.


Note:
A 'deep copy' of a linked list means we do not copy the references of the nodes of the original linked list, rather for each node in the original linked list, a new node is created.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer 'n', denoting the number of nodes in the 'linked list'.

The second line of the input contains 'n' spaced integers, representing the elements of the linked list, where 'next' pointer of 'ith' node points to 'i+1'th node.

The third line of the input contains 'n' spaced integers, where 'ith' integer 'ri' represents the index (0-indexed) of the node that the random pointer of 'ith' node points to, or '-1' if 'random' pointer points to 'null'.  


Output Format:
Return the 'head' node of final linked list.


Note:
You do not need to print anything, it has already been taken care of. Just implement the given function. We will validate your returned linked list. "True" will be printed if the linked list was cloned successfully and "False" otherwise.


Sample Input 1:
5
1 2 3 4 5
2 0 4 4 1


Sample Output 1:
True


Explanation of Sample Input 1:
For the given test case, “True” will be printed if the linked list is successfully cloned.


Sample Input 2:
2
1 2
1 0


Sample Output 2:
True


Expected Time Complexity:
Try to do this in O(n).


Expected Space Complexity:
Try to do this without using extra space.


Constraints:
1 <= n <= 5 * 10^4
-10^5 <= Node.data <= 10^5
-1 <= ri < n
Hint

Additionally, we have a HashMap that maintains the old node to new node mapping. We will need it later on.

Approaches (3)
Recursion

The basic idea is to consider the linked list like a binary tree. Every node of the Linked List has 2 pointers. So we consider these nodes as binary tree nodes. The head of the list becomes the root of the tree. So what we basically have to do now is to traverse the binary tree and clone it. The main issue that we need to tackle here is that of loops.
 

We start from the root node, and we keep traversing the tree (list), and we keep generating new nodes whenever we find a node for which the clone has not been generated. For example, we were at node A, and we used the next pointer to go to node B, and we created B’, which is a new node B with the same data. Also, say there was a random pointer from A to B. In this case, we don’t have to create yet another copy of node B because B’ already exists. We need to take care of this as well.
 

Steps are as follows:

  • Base case: If the current node is not present in the linked list, return null.
  • Check if we have already processed the current node (using HashMap), then simply return the cloned version of it.
  • Else, create a new node with the data same as the old node, i.e. copy the node.
  • Save this value in the HashMap. This is needed to avoid the loops which might be there while traversing the list.
  • Recursively copy the remaining linked list starting once from the next pointer and then from the random pointer.
  • Finally, update the next and random pointers for the new node created and return it.
Time Complexity

O(N), where ‘N’ is the number of nodes in the list.

 

In the worst case, we would be visiting 2 * N nodes i.e. N nodes using the next pointer and N nodes using the random pointer. Thus, the final time complexity is O(2 * N) = O(N).

Space Complexity

O(N), where ‘N’ is the number of nodes in the list.

 

We are using O(H) extra space for the call stack where ‘H’ is the height of the recursion tree, and height of a recursion tree could be ‘N’ in the worst case. Also, we are using a HashMap of size N to store the nodes of the list. Thus, the final space complexity is O(N + N) = O(N).

Code Solution
(100% EXP penalty)
Clone a Linked List with random pointers
Full screen
Console