输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
public Node copyRandomList(Node head) {
Node cur = head;
Map<Node,Node> map = new HashMap<>();
//建立映射关系,拷贝出带值得节点。
while (cur != null) {
map.put(cur,new Node(cur.val));
cur = cur.next;
}
//修改拷贝的next random 指向
for(Map.Entry<Node,Node> entry: map.entrySet()) {
entry.getValue().next = map.get(entry.getKey().next);
entry.getValue().random =map.get(entry.getKey().random);
}
return map.get(head);
}