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]Explanation:
A single-node linked list with value 1 and no random pointer is deep copied as-is, resulting in one new node with the same value and null random pointer.
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⁴