This content originally appeared on DEV Community and was authored by Prashant Mishra
tc :O(n) where n is no. of nodes in the original linked list
Iterative approach:
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
HashMap<Node,Node> map = new HashMap<>();
Node node = new Node(0);
Node temp = node;
while(head!=null){
Node duplicateNode = get(map,head);
temp.next = duplicateNode;
Node duplicateRandomNode = get(map, head.random);
duplicateNode.random = duplicateRandomNode;
temp = temp.next;
head = head.next;
}
return node.next;
}
public Node get(Map<Node,Node> map, Node node){
if(node ==null) return null;
if(map.containsKey(node)) return map.get(node);
map.put(node, new Node(node.val));
return map.get(node);
}
}
Recursive approach:
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
Map<Node,Node> map = new HashMap<>();
return copy(head,map);
}
public Node copy(Node node,Map<Node,Node> map){
if(node == null) return null;
if(map.containsKey(node)) return map.get(node);
map.put(node, new Node(node.val));
Node duplicate = map.get(node);
duplicate.next = copy(node.next,map);
duplicate.random = copy(node.random,map);
return duplicate;
}
}
This content originally appeared on DEV Community and was authored by Prashant Mishra
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.

APA
MLA
Prashant Mishra | Sciencx (2025-04-18T15:36:32+00:00) Copy Linked List with random pointer. Retrieved from https://www.scien.cx/2025/04/18/copy-linked-list-with-random-pointer/
" » Copy Linked List with random pointer." Prashant Mishra | Sciencx - Friday April 18, 2025, https://www.scien.cx/2025/04/18/copy-linked-list-with-random-pointer/
HARVARDPrashant Mishra | Sciencx Friday April 18, 2025 » Copy Linked List with random pointer., viewed ,<https://www.scien.cx/2025/04/18/copy-linked-list-with-random-pointer/>
VANCOUVERPrashant Mishra | Sciencx - » Copy Linked List with random pointer. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/04/18/copy-linked-list-with-random-pointer/
CHICAGO" » Copy Linked List with random pointer." Prashant Mishra | Sciencx - Accessed . https://www.scien.cx/2025/04/18/copy-linked-list-with-random-pointer/
IEEE" » Copy Linked List with random pointer." Prashant Mishra | Sciencx [Online]. Available: https://www.scien.cx/2025/04/18/copy-linked-list-with-random-pointer/. [Accessed: ]
rf:citation » Copy Linked List with random pointer | Prashant Mishra | Sciencx | https://www.scien.cx/2025/04/18/copy-linked-list-with-random-pointer/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.