剑指 Offer 35. 复杂链表的复制 - 力扣(LeetCode) (leetcode-cn.com)
题解参考:剑指 Offer 35. 复杂链表的复制(哈希表 / 拼接与拆分,清晰图解) - 复杂链表的复制 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public Node copyRandomList(Node head) {
Node cur = head;
Map<Node,Node> map = new HashMap<Node,Node>();
if(head==null){
return null;
}
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);
}
}