


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.
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.
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'.
Return the 'head' node of final linked list.
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.
5
1 2 3 4 5
2 0 4 4 1
True
For the given test case, “True” will be printed if the linked list is successfully cloned.
2
1 2
1 0
True
Try to do this in O(n).
Try to do this without using extra space.
1 <= n <= 5 * 10^4
-10^5 <= Node.data <= 10^5
-1 <= ri < n
Additionally, we have a HashMap that maintains the old node to new node mapping. We will need it later on.
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:
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).
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).