We’re preparing your current view and syncing the latest data.
You are given a linked list where each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
The input is given as the head of a linked list where each node contains a val, next pointer, and random pointer.
Return the head of the deep copied linked list.
The number of nodes in the list is in the range [0, 1000]. Each node's value is an integer.
Example 1
Input
head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output
[[7,null],[13,0],[11,4],[10,2],[1,0]]
Explanation
The original list has 5 nodes where each node's random pointer points to the index of the node it references or null. The returned list is a deep copy of the original list.