class Solution { HashMap<Node,Node> map = new HashMap<>(); public Node copyRandomList(Node head) { if(head == null) return null; Node p = head; while(p!=null){ Node q = new Node(p.val); map.put(p,q); p = p.next; } p = head; while (p!=null){ Node q = map.get(p); if(p.next!=null){ q.next = map.get(p.next); } if(p.random!=null){ q.random = map.get(p.random); } p = p.next; } return map.get(head); } }