Skip to content

Copy List with Random Pointer

MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

Each node of this linked list has a val, a next pointer, and an extra random pointer that can point to any node in the list, or to nothing. Return a deep copy: a brand-new list of new nodes with the same values, where every copied next and random points to the matching copied node. No pointer in the copy may point back into the original list.

The Node class is defined in the starter (the harness only knows ListNode). Below, each node is written as [val, index of random node].

[[7, null], [13, 0], [11, 4], [10, 2], [1, 0]]   →  a new list with the same shape
[[1, 0]]                                         →  one node whose random is itself
[]                                               →  None

Constraints

  • 0 <= number of nodes <= 1000
  • -10^4 <= Node.val <= 10^4
  • Node.random is empty or points to a node in the list.