Copy List with Random Pointer

medium

Deep copy a linked list where each node has a next and a random pointer

Copy List with Random Pointer

Key Insight

Hash map from original→clone lets you wire random pointers in a second pass

Step 1Original List
Random: 1→3, 2→1, 3→2
1
2
3
null

Each node has next and random pointers. Random: 1→3, 2→1, 3→2.

1 / 5
Practice the Code

Step-by-Step Walkthrough: Copy List with Random Pointer

Hash map from original→clone lets you wire random pointers in a second pass

  1. Original List

    Each node has next and random pointers. Random: 1→3, 2→1, 3→2.

  2. Pass 1: Create Clone Nodes

    Iterate through the list, create a clone for each node. Store original→clone in a map.

  3. Pass 2: Wire Next Pointers

    For each original node, set clone.next = map[original.next].

  4. Pass 2: Wire Random Pointers

    For each original node, set clone.random = map[original.random].

  5. Result

    Return the cloned head. Deep copy with both next and random pointers.