138. 复制带随机指针的链表 - 力扣(Leetcode)
解题思路 :
我们使用Map接口来解这个题 :
代码实现 :
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);
}
}