Explanation: Node 1's value is 1, both of its next and random pointer points to Node 2. Node 2's value is 2, its next pointer points to null and its random pointer points to itself.
Note:
You must return the copy of the given head as a reference to the cloned list.
publicclassCopyListwithRandomPointer{ /** * Copy List with Random Pointer */ publicstatic Node copyRandomList(Node head){ if (head == null) returnnull; Hashtable<Node, Node> m = new Hashtable<>();
Node p = head;
// copy all the node, put it to the map while (p != null) { Node t = new Node(); t.val = p.val; m.put(p, t); p = p.next; }
p = head; while(p != null) { m.get(p).next = m.get(p.next); m.get(p).random = m.get(p.random); p = p.next; }