Copy List with Random Pointer

MediumArrayLinked ListGraphMatrix

Description

Given an matrix head, deep copy a linked list where each node has a next pointer and a random pointer to any node or null. Return the result as a 2D array.

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,null]]
Output:[[1,null]]
Explanation:

A single-node list with value 1 and no random pointer is copied exactly, so the cloned structure is [[1,null]].

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 and sharpen your skills for technical interviews.