Copy List with Random Pointer
Return a deep copy of the list (new nodes only).
Copy List with Random Pointer is Clone Graph wearing a linked-list costume: random can point anywhere, so you cannot copy in one naive pass. A map from old node to new node lets you wire next and random after every copy exists. The O(1)-space interleaving trick is the flex once the map version clicks.
Examples
You write
copy_random_list(head: Optional['Node'])
Example 1
- Input
- [[7,null],[13,0],[11,4],[10,2],[1,0]]
- returns
- [[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2
- Input
- [[1,1],[2,1]]
- returns
- [[1,1],[2,1]]
The pattern behind it
Linked Lists
I reach for it when
- Nodes point to the next node, and you cannot index into the list.
- You must reverse, merge, reorder or remove a node.
Say this before you type. Say which pointers you hold and which link you are about to change.
All 12 Linked Lists problemsArrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.