A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
Construct a deep copy of the list. The deep copy should consist of exactly nbrand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
Return the head of the copied linked list.
The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
val: an integer representing Node.val
random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.
Your code will only be given the head of the original linked list.
Example 1:
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:
Input: head = [[1,1],[2,1]]
Output: [[1,1],[2,1]]
Example 3:
Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]]
Constraints:
0 <= n <= 1000
-104 <= Node.val <= 104
Node.random is null or is pointing to some node in the linked list.
Solutions
Solution 1: Hash Table
We can define a dummy head node $\textit{dummy}$ and use a pointer $\textit{tail}$ to point to the dummy head node. Then, we traverse the linked list, copying each node and storing the mapping between each node and its copy in a hash table $\textit{d}$, while also connecting the $\textit{next}$ pointers of the copied nodes.
Next, we traverse the linked list again and use the mappings stored in the hash table to connect the $\textit{random}$ pointers of the copied nodes.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the linked list.
/*// Definition for a Node.class Node {public: int val; Node* next; Node* random; Node(int _val) { val = _val; next = NULL; random = NULL; }};*/classSolution{public:Node*copyRandomList(Node*head){Node*dummy=newNode(0);Node*tail=dummy;unordered_map<Node*,Node*>d;for(Node*cur=head;cur;cur=cur->next){Node*node=newNode(cur->val);tail->next=node;tail=node;d[cur]=node;}for(Node*cur=head;cur;cur=cur->next){d[cur]->random=cur->random?d[cur->random]:nullptr;}returndummy->next;}};
/** * Definition for a Node. * type Node struct { * Val int * Next *Node * Random *Node * } */funccopyRandomList(head*Node)*Node{dummy:=&Node{}tail:=dummyd:=map[*Node]*Node{}forcur:=head;cur!=nil;cur=cur.Next{node:=&Node{Val:cur.Val}d[cur]=nodetail.Next=nodetail=node}forcur:=head;cur!=nil;cur=cur.Next{ifcur.Random!=nil{d[cur].Random=d[cur.Random]}}returndummy.Next}
/*// Definition for a Node.public class Node { public int val; public Node next; public Node random; public Node(int _val) { val = _val; next = null; random = null; }}*/publicclassSolution{publicNodeCopyRandomList(Nodehead){Dictionary<Node,Node>d=newDictionary<Node,Node>();Nodedummy=newNode(0);Nodetail=dummy;for(Nodecur=head;cur!=null;cur=cur.next){Nodenode=newNode(cur.val);tail.next=node;tail=node;d[cur]=node;}for(Nodecur=head;cur!=null;cur=cur.next){if(cur.random!=null){d[cur].random=d[cur.random];}}returndummy.next;}}
Solution 2: Simulation (Space Optimization)
In Solution 1, we used an additional hash table to store the mapping between the original nodes and the copied nodes. We can also achieve this without using extra space, as follows:
Traverse the original linked list, and for each node, create a new node and insert it between the original node and the original node's next node.
Traverse the linked list again, and set the $\textit{random}$ pointer of the new node based on the $\textit{random}$ pointer of the original node.
Finally, split the linked list into the original linked list and the copied linked list.
The time complexity is $O(n)$, where $n$ is the length of the linked list. Ignoring the space occupied by the answer linked list, the space complexity is $O(1)$.
/*// Definition for a Node.class Node {public: int val; Node* next; Node* random; Node(int _val) { val = _val; next = NULL; random = NULL; }};*/classSolution{public:Node*copyRandomList(Node*head){if(!head){returnnullptr;}Node*cur=head;while(cur!=nullptr){Node*node=newNode(cur->val);node->next=cur->next;cur->next=node;cur=node->next;}cur=head;while(cur!=nullptr){cur->next->random=cur->random==nullptr?nullptr:cur->random->next;cur=cur->next->next;}cur=head;Node*ans=head->next;while(cur->next!=nullptr){Node*node=cur->next;cur->next=node->next;cur=node;}returnans;}};
/** * Definition for a Node. * type Node struct { * Val int * Next *Node * Random *Node * } */funccopyRandomList(head*Node)*Node{ifhead==nil{returnnil}forcur:=head;cur!=nil;{node:=&Node{cur.Val,cur.Next,nil}cur.Next=nodecur=node.Next}forcur:=head;cur!=nil;cur=cur.Next.Next{ifcur.Random!=nil{cur.Next.Random=cur.Random.Next}}ans:=head.Nextforcur:=head;cur.Next!=nil;{node:=cur.Nextcur.Next=node.Nextcur=node}returnans}
/*// Definition for a Node.public class Node { public int val; public Node next; public Node random; public Node(int _val) { val = _val; next = null; random = null; }}*/publicclassSolution{publicNodeCopyRandomList(Nodehead){if(head==null){returnnull;}Nodecur=head;while(cur!=null){Nodenode=newNode(cur.val);node.next=cur.next;cur.next=node;cur=node.next;}cur=head;while(cur!=null){cur.next.random=cur.random==null?null:cur.random.next;cur=cur.next.next;}cur=head;Nodeans=head.next;while(cur.next!=null){Nodenode=cur.next;cur.next=node.next;cur=node;}returnans;}}