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.
/* // Definition for a Node. class Node { public int val; public Node next; public Node random; public Node() {} public Node(int _val,Node _next,Node _random) { val = _val; next = _next; random = _random; } }; */ classSolution{ public Node copyRandomList(Node head){ if (head == null) { return head; } Map<Node, Node> map = new HashMap<>(); map.put(null, null); Node cur = head; while (cur != null) { if (!map.containsKey(cur)) { Node copy = new Node(cur.val); map.put(cur, copy); } cur = cur.next; } cur = head; while (cur != null) { map.get(cur).next = map.get(cur.next); map.get(cur).random = map.get(cur.random); cur = cur.next; } return map.get(head); } }