题目描述
题目链接138. 复制带随机指针的链表
题解
哈希表:
key是原始节点,value是复制的节点。思路挺简单的,直接看代码就能理解了
class Solution {
public Node copyRandomList(Node head) {
Map<Node, Node> map = new HashMap<>();
Node cur = head;
while (cur != null){
map.put(cur, new Node(cur.val));
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);
}
}