← 返回 snowflake 的题目列表Copy List with Random Pointer
类型:qbank
A linked list of length n is represented in this format for testing: nodes[i] = [val, randomIndex] val is the value of the ith node.
Problem Overview: Copy List with Random Pointer
You are given a linked list containing n items. To make testing easier, the list is presented in a specific array format:
nodes[i] = [val, randomIndex]
val: This is the value stored inside the ith node.
randomIndex: This is the index of the node that the random pointer connects to. If the random pointer is null, this value is -1.
Your task is to create and return a deep copy of this list.
The new list must be an exact independent clone. It must keep the same order for next pointers and the exact same connections for random pointers as the original list.
Sample Cases
Case 1:
Input: nodes = [[7,-1],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,-1],[13,0],[11,4],[10,2],[1,0]]
Case 2:
Input: nodes = [[1,1],[2,1]]
Output: [[1,1],[2,1]]
Input Rules and Limits
0 <= nodes.length <= 1000 (The list can have up to 1000 nodes).
-10^4 <= val <= 10^4 (Node values are between -10,000 and 10,000).
randomIndex is either -1 or a valid index inside the list (0 <= randomIndex < nodes.length).