FreeCareerPath
Build the PrimitiveMedium

Copy List with Random Pointer

Each node has a `next` and a `random` pointer that may target any node or `None`. Write `copy_random_list(head)` returning a deep copy: entirely new nodes, with the same structure. The difficulty is that a random pointer may target a node you have not created yet. Two passes with a map from original node to copy solves it. The O(1)-space version interleaves each copy directly after its original, so `copy.random = original.random.next` — worth knowing, and worth saying you know.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Copy List with Random Pointer" are saved to your account — logging in takes a few seconds.

Copy List with Random Pointer — FreeCareerPath