public Node copyRandomList(Node head){
if(head == null) return null;
Node cur = head;
Map<Node,Node> map = new HashMap<>();
while(cur != null){
Node node = new Node(cur.val);
map.put(cur,node);
cur = cur.next;
}
// 说明已经把旧和新的结点以键值对的形式存放到map当中了
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);
}
Java实现对带有随机指针的链表进行复制
最新推荐文章于 2021-07-22 16:58:08 发布