Skip to content
AI360Xpert
Beta
Difficulty: MediumLinked List

Copy List with Random Pointer

Problem in Plain English

You are given a linked list where each node also has a random pointer to any node or null. Build a deep copy with brand-new nodes that preserves every next and random connection.

Problem Statement

A linked list of length n is given such that each node contains an additional random pointer, which can point to any node in the list or to null.

Construct a deep copy of the list with exactly n brand-new nodes whose values match the originals and whose next and random pointers mirror the original structure without referencing any original node. Return the head of the copied list.

The displayed pairs are [value, random_index], where indices follow the next chain from zero. Your function receives actual nodes through head, not the pair array. The next chain terminates at null; random links may point backward, forward, to self, or form cycles.

Constraints

  • 0 <= n <= 1000
  • -10^4 <= Node.val <= 10^4
  • Node.random is null or points to some node in the same list.

Examples

Example 1

Input
head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output
[[7,null],[13,0],[11,4],[10,2],[1,0]]

Label originals O0 through O4 in next order, with values 7,13,11,10,1. Fresh nodes C0 through C4 have next links C0 -> C1 -> C2 -> C3 -> C4 and random targets null,C0,C4,C2,C0. The pair array looks identical, but every node and non-null target belongs exclusively to the new list.

Example 2

Input
head = [[1,1],[2,1]]
Output
[[1,1],[2,1]]

C0.random and C1.random both target C1. The second node points randomly to itself, so its copy must also point to itself rather than to O1; shared targets remain shared.

Example 3

Input
head = []
Output
[]

An empty list copies to an empty list.

Example 4

Input
head = [[3,1],[3,0],[3,null]]
Output
[[3,1],[3,0],[3,null]]

The first two equal-valued nodes form a cycle through random links. Their distinct copies preserve that cycle, while the third copy has a null random. Mapping by value would collapse three different identities incorrectly.

Intuition

Copying values is only half the job: every pointer must reach the corresponding new node, never an original. In Example 1, the copy of 11 must point randomly to the copy of 1 even though that target appears later. Give each original a unique clone first, then translate its connections. A map stores that correspondence explicitly; placing each clone immediately after its original stores the same correspondence in the next links temporarily. Equal values cannot identify nodes, and cycles among random links require no recursion because traversal follows only next.

Approaches

Hash Map Two Passes

Solution Details

Reveal Hash Map Two Passes: intuition, complexity, and code

Hints

Hint 1
A clone whose random points forward cannot be wired until that target clone exists, so separate creation from wiring.
Hint 2
First map every original node to a fresh copy, then translate each next and random through the map with null mapping to null.
Hint 3
To drop the map, weave each copy after its original so original.random.next is exactly the clone the random link needs.

Edge Cases

  • An empty head returns null immediately without entering any loop.
  • A node whose random points to itself must have its clone point to itself, not to the original.
  • Several nodes sharing one random target must all resolve to the same single clone.
  • Equal-valued nodes are distinct identities; random cycles and shared targets must be preserved inside the copied list.
  • A 1000-node list with randoms pointing backward or forward still needs exactly 1000 fresh nodes; random edges do not drive traversal.

Common Mistakes and Interview Tips

  • Reading curr.random.next without a null check, which crashes on nodes whose random is null.
  • Leaving the original list interwoven after the split instead of restoring its next chain.
  • Shallow-copying by reusing original nodes for next or random instead of their mapped clones.
  • Keying the map by value rather than node identity, which merges distinct equal-valued nodes.
  • Separating pairs before finishing the random pass, so target.next no longer always denotes its clone.
  • Following random edges during traversal without tracking visits; a self pointer or random cycle can then loop forever.

Key Takeaway

When clones must preserve cross-links, either map originals to copies first or weave copies inline so each target is reachable without a map.