Copy Linked List with random pointer

Problem

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 = …


This content originally appeared on DEV Community and was authored by Prashant Mishra

Problem

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
APA

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/

MLA
" » 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/
HARVARD
Prashant 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/>
VANCOUVER
Prashant 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.

You must be logged in to translate posts. Please log in or register.