Copy List with Random Pointer

Medium

Description

Deep copy a linked list where each node has a next pointer and a random pointer to any node or null.

Examples

Input:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output:[[7,null],[13,0],[11,4],[10,2],[1,0]]
Explanation:

Deep copy preserves structure.

Input:head = [[1]]
Output:[1]
Explanation:

Minimal case with a single-element matrix.

Input:head = [[3,2],[5,null],[8,1],[12,0]]
Output:[[3,2],[5,null],[8,1],[12,0]]
Explanation:

A 4-node list where node 0 points randomly to node 2, node 1's random pointer is null, node 2 points randomly to node 1, and node 3 points randomly to node 0. The deep copy maintains all these random pointer relationships while creating entirely new nodes.

Constraints

  • 0 ≤ n ≤ 1000
  • -10⁴ ≤ Node.val ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!