Skip to content
Problem 138 · LeetCode 138(opens in a new tab)

Copy List with Random Pointer

MediumHash tables

You get the head of a linked list in which each node has a value, a next link and a second link, random. The random link can point at any node of the same list, at the node itself, or at null.

Build an independent copy of the whole list: one new node per original node, with the same values, whose next and random links point at the matching new nodes. No link in the copy may lead back into the original list. Return the head of the copy.

In the examples a list is written as one pair per node, [value, index of the random target], with null when the random link is empty.

Example 1

Input:  head = [[4,2],[8,null],[6,0]]
Output: [[4,2],[8,null],[6,0]]
Why:    the copy has the same shape, built from new nodes
2 more examples and the constraints

Example 2

Input:  head = [[5,0]]
Output: [[5,0]]
Why:    the copy’s random link must point at the copy itself

Example 3

Input:  head = []
Output: []
Why:    an empty list copies to an empty list

Constraints

  • 0 ≤ number of nodes ≤ 1,000
  • Every value is between −10,000 and 10,000
  • Each random link is null or points at a node of this list

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Hash map, two passesO(n)
  2. Weave copies into the listO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 1,000 nodesVerdict
Hash map, two passesO(n)
Passes; easiest to explain
Weave copies into the listO(n)
Best; no extra map

n = how many nodes the list has. Both are linear; the weave saves the O(n) map.

Edge cases to test

  • Empty list. A null head returns null; no pass should touch head.next.
  • Random link to itself. [[5,0]]: the copy must point at the copy, not at the original 5.
  • Random links pointing forward. The target is copied later, which is why one pass with no lookup fails.
  • Equal values. [[3,null],[3,0],[3,1]]: map nodes by identity, never by value.

Common mistakes

  • Setting copy.random = node.random, so the copy points back into the original list.
  • Keying the map by value, which breaks as soon as two nodes share a value.
  • Forgetting to restore the original next links after weaving.

How to talk through it

  1. 1Clarify. Can random point at the node itself or at null? May I change the input list if I put it back?
  2. 2Explain the problem with one pass. A random link can point at a node not copied yet, so a lookup from original to copy is needed.
  3. 3Give the map version. Two passes with a map: O(n) time and O(n) extra space.
  4. 4Weave to save space. Put each copy after its original so node.next is the lookup, then unweave and restore.
  5. 5Test out loud. Run the empty list, a self-pointing node, and a random link that points forward.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?