Every node of this list has a val, a next pointer and a random pointer that
can point to any node of the list (or to null). Write copyRandomList(head) that
returns a deep copy: brand-new nodes whose next and random pointers reference
other *new* nodes, never the originals.
solve(pairs) is given for you. pairs[i] = [val, randomIndex] describes node i
(randomIndex is the index of the node its random points to, or null). It builds the
list, calls your function, and returns:
copy — the copy described with the same [val, randomIndex] pairs;sharesNodes — true if any node reachable from the copy is an original node.A correct answer returns the same pairs and sharesNodes: false.
Hint: a Map from original node to copied node solves it in two passes (O(n) space).
Interleaving each copy right after its original avoids the map (O(1) extra space).
Example
solve([[7,null],[13,0],[11,4],[10,2],[1,0]])
→ { copy: [[7,null],[13,0],[11,4],[10,2],[1,0]], sharesNodes: false }Sample tests