Copy List with Random Pointer
Given a linked list where each node has a next pointer and a random pointer that can point to any node in the list or to None, build a deep copy: a brand-new set of nodes whose next and random pointers mirror the original structure. Return the head of the copied list.
Open official problem prompt ↗Produce an independent copy of the list whose next and random pointers replicate the original wiring but reference only new nodes.
Photocopy every page of a book first, then redraw the cross-references so each copied page points to other copied pages, never the originals.
- Input
- head = [[7,null],[13,0],[11,4],[10,2],[1,0]] (each pair is [val, random_index])
- Output
- [[7,null],[13,0],[11,4],[10,2],[1,0]]
- Why
- The copy has the same values and the same next/random wiring, but every node is a freshly allocated object.
0 <= n <= 1000-10^4 <= Node.val <= 10^4Node.random is null or points to a node in the list