Copy List with Random Pointer

Difficulty: Medium

Each node in this linked list has two pointers: the usual "next", and a "random" pointer that can point to any node in the list (including itself) or to null.

Make a fully independent deep copy: same values in the same order, with each copy's "random" pointer aimed at the matching copy, not the original node. The copy must not share any nodes with the original.

Examples

Input: A 3-node list with values [1, 2, 3]. random: node0 -> node2, node1 -> node0, node2 -> node1.
Output: A separate copy with values [1, 2, 3] where copy0.random -> copy2, copy1.random -> copy0, copy2.random -> copy1.

Every random link in the copy points at a node inside the copy, mirroring the pattern of the original.

Input: A 2-node list with values [1, 2]. random: node0 -> null, node1 -> node1 (points at itself).
Output: A separate copy with values [1, 2] where copy0.random -> null and copy1.random -> copy1.

Input: head = null (empty list)
Output: null

Constraints

  • The number of nodes is between 0 and 1000.

  • -10^4 <= Node.val <= 10^4

  • random is null or points at a node in the same list.

Approach

The direct approach copies every node (no pointers yet) and uses a hash map from each original to its copy. A second pass uses the map to set each copy's "next" and "random" pointers. It's simple, but the map uses memory proportional to the list's length.

The neater approach skips the map by inserting each copy right after its original in the list. Then "the copy of X" is always X.next, so random pointers can be wired with no lookup. A final pass splits the list back into the original and the copy.

Solutions

Brute Force — Hash Map from Original to Copy

First pass: copy each node's value and store "original -> copy" in a map. Second pass: for each original, use the map to point its copy's next and random at the copies of the original's next and random targets.

class Node {
  constructor(val, next = null, random = null) {
    this.val = val;
    this.next = next;
    this.random = random;
  }
}

function copyRandomList(head) {
  if (!head) return null;

  const copyOf = new Map(); // original node -> its copy

  let curr = head;
  while (curr) {
    copyOf.set(curr, new Node(curr.val));
    curr = curr.next;
  }

  curr = head;
  while (curr) {
    const copy = copyOf.get(curr);
    copy.next = curr.next ? copyOf.get(curr.next) : null;
    copy.random = curr.random ? copyOf.get(curr.random) : null;
    curr = curr.next;
  }

  return copyOf.get(head);
}

Time: O(n) · Space: O(n) — the hash map

Optimal — Interleave Copies In Place

Step 1: create each node's copy and splice it in right after the original, so the list reads original1 -> copy1 -> original2 -> copy2 -> ...

Step 2: "the copy of X" is now X.next, so set each copy's random pointer to (original's random node).next — that random node's copy.

Step 3: walk the list again and unhook the copies, restoring the original list and producing the standalone copy.

class Node {
  constructor(val, next = null, random = null) {
    this.val = val;
    this.next = next;
    this.random = random;
  }
}

function copyRandomList(head) {
  if (!head) return null;

  // Step 1: interleave a copy after every original node.
  let curr = head;
  while (curr) {
    const copy = new Node(curr.val, curr.next);
    curr.next = copy;
    curr = copy.next;
  }

  // Step 2: wire up random pointers using the interleaving.
  curr = head;
  while (curr) {
    if (curr.random) {
      curr.next.random = curr.random.next;
    }
    curr = curr.next.next;
  }

  // Step 3: split the interleaved list back into two separate lists.
  const dummy = new Node(0);
  let copyCurr = dummy;
  curr = head;
  while (curr) {
    const copy = curr.next;
    curr.next = copy.next; // restore original list's next pointer
    copyCurr.next = copy;
    copyCurr = copy;
    curr = curr.next;
  }

  return dummy.next;
}

Time: O(n) · Space: O(1) extra — aside from the copied nodes themselves