Medium · Linked Lists

Copy List with Random Pointer

Given the head of a linked list whose nodes each have a next pointer and a random pointer (to any node of the list or null), return a deep copy: new nodes with the same values whose next and random pointers mirror the original's and never point into the original list. In the samples, random[i] is the 0-based index of node i's random target (∅ = null).

Examples

Example 1

[7,13,11,10] rnd→[∅,0,3,2]

Output: copy [7,13,11,10]

Example 2

[1,2] rnd→[1,1]

Output: copy [1,2]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Linked Lists problems