Clone a Linked List with Random Pointers

Asked atApple
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Explain the map solution first.
  2. 02Map every original node to a clone.
  3. 03Wire next and random through the map.
  4. 04Optionally interleave clones for O(1) extra space.
  5. 05Separate the two lists carefully.

Reference answer

Then expect these follow-ups

  • Why does the interleaving trick avoid a map?

    Tests: correctness reasoning

  • How would this generalize to an arbitrary graph?

    Tests: follow-up reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question