advanced8 min read·Updated September 23, 2026
Copy List with Random Pointer Explained: 7→13→11→10→1 Clone Trace
Master the Copy List with Random Pointer problem with a step-by-step trace of 7→13→11→10→1. Learn the interleaving mental model for $O(1)$ space cloning.
By Learnisim AI·Published September 23, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Singly linked list basics
- Pointer manipulation
- Basic hash map concepts
Why
Why copying a linked list with random pointers breaks standard cloning
Phase 1: The Broken Clone Trap
Imagine you are handed a linked list where every node has two pointers: a standard
next pointer and a random pointer that can reference any node in the list or be null. Specifically, consider our working example: nodes are arranged in the exact order 7 13 11 10 1, with random pointers mapping 7 null, 13 7, 11 1, 10 11, and 1 7.If you try to copy this structure using a naive two-pass approach—building the
next chain first, then attempting to assign the random pointers—you immediately hit a wall. When you look at the clone of node 13, you know its original random points to node 7. But in your newly allocated list, which node corresponds to 7? Without keeping track of object memory addresses or modifying the structure, you cannot simply look up an arbitrary node in time.Trying
Model
Building the Interleaved Interweaving Mental Model
Phase 2: The Interleaved Node Model
When we look at our locked working example of copying the list , a standard deep copy fails because a node's pointer might reference a node we haven't visited yet, or it might point backward in a way our single pass cannot resolve. To solve this without a secondary hash map, we use an interleaved node model that relies entirely on physical structural placement.
Imagine taking our original list and inserting each newly created clone directly adjacent to its original counterpart. For instance, node is immediately followed by its clone , which is then followed by original node , then its clone , and so on. In this interwoven topology, the relationship between an original node and its clone becomes deterministic: if an original node is at pointer , its clone is always at .
This physical proximity solves the pointer mystery. If original node has a pointer targeting original node , where does clone 's pointer need to point? It must point to clone . Since clone sits immediately after original node (meaning at ), we can find the exact target for our clone's pointer by inspecting .
Worked example
Tracing the Interleaving Copy Algorithm Step-by-Step
Phase 3: Worked Example
To see how the interweaving mental model operates in practice, let us trace our locked example: cloning the list where the random pointers are given by , , , , and .
Given:
- Original nodes in sequence:
- Original next pointers:
- Original random pointers: , , , ,
- Original nodes in sequence:
- Original next pointers:
- Original random pointers: , , , ,
Steps:
- State after Pass 1:
2. Pass 2: Assign random pointers. For each original node , its clone is . Therefore, must point to the clone corresponding to . Using our identity formula, (if is not null).
- For : is , so .
- For : is 7, so .
- For : is 1, so .
- For : is 11, so .
- For : is 7, so .
- Original list:
- Cloned list:
1.Pass 1: Interleave clone nodes. We insert a duplicate clone immediately after every original node.
- State after Pass 1:
2. Pass 2: Assign random pointers. For each original node , its clone is . Therefore, must point to the clone corresponding to . Using our identity formula, (if is not null).
- For : is , so .
- For : is 7, so .
- For : is 1, so .
- For : is 11, so .
- For : is 7, so .
3.Pass 3: Unweave the lists. We separate the interleaved chain back into two independent lists by restoring original next pointers and assigning clone next pointers.
- Original list:
- Cloned list:
Result:
A fully independent cloned list whose random pointers match the expected shape (, , , , ), completely isolated from the original memory locations.
A fully independent cloned list whose random pointers match the expected shape (, , , , ), completely isolated from the original memory locations.
Try this: Given the intermediate state after Pass 1: 7 -> 7' -> 13 -> 13' -> 11 -> 11' -> 10 -> 10' -> 1 -> 1' -> null, and the original random pointer 10 -> 11, write out the exact pointer assignment expression used in Pass 2 to set 10'.random.
Practice
Predicting the Intermediate State During Pass 2
Phase 4: Practice
Now that you have seen how the three-pass interweaving algorithm handles the locked sequence of nodes , it is time to check your mental model of the wiring phase. Recall that Pass 2 is responsible for establishing the
random pointers of every cloned node by looking directly at the adjacent original node's random pointer.Consider the cloned node for 13, let us call it . According to our locked working example, the original node 13 has a random pointer directed at 7. When Pass 2 executes, how do we correctly assign using the interleaved structure where every original node is immediately followed by its clone?
Work through the pointer adjustment for using the interleaved state formed after Pass 1, keeping in mind how
original.random.next reaches the corresponding clone.Apply
Applying the Interleaving Pointer Pattern to New Graph Topologies
Phase 5: Apply
Now that you have successfully cloned our locked sequence using the three-pass interweaving pattern, let us see how this exact same mental model transfers to a more extreme pointer topology. Imagine you are given a graph where every single node's random pointer creates a strict cycle, such that node , or a scenario where all random pointers are null.
By keeping the foundational three-pass procedure intact—interleave clones, wire random pointers via
orig.random.next, and unweave—you eliminate the need for any auxiliary hash map storage, keeping space complexity at . When faced with a new pointer-doubling problem in interviews or systems programming, look for whether you can temporarily augment the existing memory layout rather than allocating a separate index dictionary.Phase 5: Practical Vignette
Suppose you encounter a variant of our locked list where every node's
random pointer points directly to itself instead of pointing to other nodes in the sequence . Walk through Pass 2 under this self-referential topology: what does clone.random evaluate to when orig.random is orig itself?FAQ
How do random pointers break standard linked list cloning?
Standard cloning only copies the 'next' pointer iteratively. When a node has a 'random' pointer pointing ahead or behind, the target node of that random pointer might not have been created yet in the new list, leading to broken references or requiring an hash map.
How does the interleaved interweaving mental model handle random pointers in the 7→13→11→10→1 example?
In Pass 1, we create clone nodes and weave them directly after their originals (e.g., 7 → 7' → 13 → 13'). Because every clone sits immediately next to its original, finding the clone of any random target is trivial: original.random.next gives the corresponding clone.
What is the time and space complexity of the interleaved cloning approach?
The time complexity is because we traverse the list three times (interweaving, assigning random pointers, and separating the lists). The space complexity is auxiliary space since we reuse the existing list structure instead of using a hash map.
What happens during Pass 2 of the interleaving copy algorithm?
During Pass 2, we iterate through the interleaved list to wire up the random pointers. For each original node, its clone's random pointer is set using 'original.random.next', safely linking to the newly created clone node.