public Node copyRandomList(Node head) {
if(head == null) {
return null;
}
Node cur = head;
Map<Node, Node> map = new HashMap<>();
// 复制各节点
while(cur != null) {
map.put(cur, new Node(cur.val));
cur = cur.next;
}
cur = head;
// 构建新链表的 next 和 random 指向
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);
}
剑指Offer-35复杂链表的复制
最新推荐文章于 2022-06-12 20:02:44 发布