← DiffPush

Copy LL with random pointers

DiffPush Tier Linked List · Hard Promblems of LL O(n) · O(n) for the n new nodes (the interleaving itself uses no map)

Cloning the Relay Team's Cross-Wired Call Sheet

A relay network stores each runner's order plus a private walkie channel that can jump to any runner — or nobody at all. The operations team needs an entirely separate clone of the network: same runner values, same handoff order, and each clone's jump channel pointing at the equivalent clone runner, never at an original. In a single weaving pass, each new runner is stitched in right behind their original; the jump channels are copied by reading the neighbour of whatever the original points at; then the weave is split back into two clean, independent chains.

Input: An array pairs of n entries [value, random_index], where random_index is the 0-based position of the random target or -1 for null.

Output: The deep copy expressed the same way — n pairs [value, random_index] over brand-new nodes.

Constraints

Examples

Example 1

Input: {"pairs":[[7,-1],[13,0],[11,4],[10,2],[1,0]]}
Output: [[7,-1],[13,0],[11,4],[10,2],[1,0]]
Every jump channel is reproduced inside the clone — e.g. the second runner's channel re-points at the clone of runner 0, never at the original.

Example 2

Input: {"pairs":[[1,1],[2,1]]}
Output: [[1,1],[2,1]]
Both channels land on the same (second) runner, and the second runner's channel loops back to itself.

Solve this in your browser →

Also on LeetCode ↗