Redian新闻
>
Leetcode Copy List with Random Pointer Runtime Error?
avatar
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;
}
avatar
c*w
2
第二个while循环
copy random的时候
如果原random是null怎么办
不就run time error了
所以要处理
if(pointer.random!=null)
avatar
S*0
3
谢谢,我真是粗心。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。