Copy List with Random Pointer
Problem
You are given the head of a singly linked list in which every node carries two pointers: next, which links the nodes in sequence, and random, which points to any node in the list or to nothing at all. Produce a deep copy of the list: a set of brand-new nodes with the same values, whose next and random pointers reproduce the original structure but reference only new nodes. Return the head of the copy. The original list must not be damaged when you return.
- The list has between 0 and 2,000 nodes.
- Node values fit in a signed 32-bit integer.
- Each
randomisNoneor points to some node in the same list (possibly the node itself).
Examples
- Nodes
A(4) -> B(9) -> C(2) -> D(6)withA.random = C,B.random = None,C.random = A,D.random = D→ a new listA'(4) -> B'(9) -> C'(2) -> D'(6)withA'.random = C',B'.random = None,C'.random = A',D'.random = D'. Random pointers may go forward, backward, to self, or nowhere, and in the copy they must land on copied nodes, not the originals. A(8) -> B(3)withA.random = A,B.random = A→A'(8) -> B'(3)withA'.random = A',B'.random = A'.- empty list → empty list.
Hints
- Copying
nextpointers is a simple walk. What makesrandomhard? Its target may not have been copied yet. - If you could look up "the copy of node X" in O(1), a second pass could fill in all
randompointers. - Either build a map from original to copy in one pass and wire pointers in a second, or interleave each copy right after its original so that
copy = original.nextserves as the map, then unweave.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.