class Solution {
public Node copyRandomList(Node head) {
if(head == null){
return null;
}
Node cur = head;
while(cur != null){
Node tmp = new Node(cur.val);
tmp.next = cur.next;
cur.next = tmp;
cur = tmp.next;
}
Node curNode = head;
while(curNode != null){
if(curNode.random != null){
curNode.next.random = curNode.random.next;
}
curNode = curNode.next.next;
}
Node old = head;
Node newnode = head.next;
Node result = head.next;
while(newnode.next != null){
old.next = old.next.next;
newnode.next = newnode.next.next;
old = old.next;
newnode = newnode.next;
}
old.next = null;
return result;
}
}
作者:Code_respect
链接:https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/solution/jiu-lian-biao-li-fu-zhi-xin-biao-ran-hou-c7is/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。