Loading problem...
You are given a specialized linked list of length n where each node contains three components: a value, a pointer to the next node in the sequence, and an arbitrary pointer that can reference any node in the list (including itself) or be null.
Your task is to construct an independent replica of this linked list. The replica must be a completely new structure consisting of exactly n freshly created nodes. Each new node should have:
Critical requirement: The replica must be entirely self-contained. No pointer in the new list should reference any node from the original list. If node A in the original list has its arbitrary pointer pointing to node B in the original list, then in the replica, the copy of A must have its arbitrary pointer pointing to the copy of B.
Input/Output Representation:
The linked list is represented as an array of n pairs. Each pair is formatted as [value, arbitrary_index] where:
value: The integer value stored in the nodearbitrary_index: The 0-based index of the node that the arbitrary pointer references, or null if the arbitrary pointer doesn't point to any nodeYour function receives the head of the original linked list and must return the head of the newly created replica.
head = [[7,null],[13,0],[11,4],[10,2],[1,0]][[7,null],[13,0],[11,4],[10,2],[1,0]]The linked list has 5 nodes. Node 0 (value 7) has no arbitrary link. Node 1 (value 13) has its arbitrary pointer pointing to node 0. Node 2 (value 11) points arbitrarily to node 4. Node 3 (value 10) points arbitrarily to node 2. Node 4 (value 1) points arbitrarily to node 0. The replica preserves all these relationships but uses entirely new node instances.
head = [[1,1],[2,1]][[1,1],[2,1]]Both nodes have their arbitrary pointers pointing to node 1 (the second node). In this case, both the first and second nodes reference the same target through their arbitrary pointers, and this relationship is preserved in the replica.
head = [[3,null],[3,0],[3,null]][[3,null],[3,0],[3,null]]All three nodes have the same value (3). Only the second node (index 1) has an arbitrary pointer, which points to the first node (index 0). The first and third nodes have null arbitrary pointers. Despite identical values, each node is distinct and must be replicated as a separate new node.
Constraints