Leetcode Copy List with Random Pointer Runtime Error?# JobHunting - 待字闺中
S*0
1 楼
话不多说直接上代码,在本地测了没发现问题。。
谢谢.....
public static RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(head == null) return null;
RandomListNode pointer, res_pointer;
pointer = head;
while(pointer != null){
RandomListNode temp = new RandomListNode(pointer.label);
temp.random = null;
temp.next = pointer.next;
RandomListNode next = pointer.next;
pointer.next = temp;
pointer = next;
}
pointer = head;
while(pointer != null){
pointer.next.random = pointer.random.next;
pointer = pointer.next.next;
}
pointer = head;
RandomListNode res = head.next;
res_pointer = res;
while(pointer != null && res_pointer != null){
pointer.next = pointer.next.next;
if(res_pointer.next != null) res_pointer.next = res_pointer.next
.next;
pointer = pointer.next;
res_pointer = res_pointer.next;
}
return res;
}
谢谢.....
public static RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(head == null) return null;
RandomListNode pointer, res_pointer;
pointer = head;
while(pointer != null){
RandomListNode temp = new RandomListNode(pointer.label);
temp.random = null;
temp.next = pointer.next;
RandomListNode next = pointer.next;
pointer.next = temp;
pointer = next;
}
pointer = head;
while(pointer != null){
pointer.next.random = pointer.random.next;
pointer = pointer.next.next;
}
pointer = head;
RandomListNode res = head.next;
res_pointer = res;
while(pointer != null && res_pointer != null){
pointer.next = pointer.next.next;
if(res_pointer.next != null) res_pointer.next = res_pointer.next
.next;
pointer = pointer.next;
res_pointer = res_pointer.next;
}
return res;
}