a**********0 发帖数: 422 | 1 检查了好几遍 没发现错误
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head == null)
return null;
if(head.next == null){
RandomListNode result = new RandomListNode(head.label);
result.next = head.next;
result.random = result.random;
return result;
}
RandomListNode scanner = head;
while(scanner != null){
RandomListNode temp = new RandomListNode(scanner.label);
temp.next = scanner.next;
scanner.next = temp;
scanner = scanner.next.next;
}
scanner = head;
while(scanner != null){
if(scanner.random != null)
scanner.next.random = scanner.random.next;
scanner = scanner.next.next;
}
RandomListNode fakeHead = head.next;
scanner = fakeHead;
while(scanner.next != null){
scanner.next= scanner.next.next;
scanner = scanner.next;
}
return fakeHead;
}
} |
m*********n 发帖数: 931 | 2 对java不熟 我感觉好像有点问题比如 这段代码:
if(head.next == null){
RandomListNode result = new RandomListNode(head.label);
result.next = head.next;
result.random = result.random;
return result;
}
result是一个新的node 然后result的next连null, result.random指向result.random
是什么意思? |
a**********0 发帖数: 422 | 3 意见很中肯
不过我已经通过了
你说的我改掉了
random
【在 m*********n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 对java不熟 我感觉好像有点问题比如 这段代码: : if(head.next == null){ : RandomListNode result = new RandomListNode(head.label); : result.next = head.next; : result.random = result.random; : return result; : } : result是一个新的node 然后result的next连null, result.random指向result.random : 是什么意思?
|
a**********0 发帖数: 422 | 4 我的思路是
复制每个节点 插在每个节点的后边
然后每个都copy random pointer
然后分开两个链表
我为什么第一次没有通过呢 因为第一次只是构建了一个新链表 但是把原来的破坏了
random
【在 m*********n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 对java不熟 我感觉好像有点问题比如 这段代码: : if(head.next == null){ : RandomListNode result = new RandomListNode(head.label); : result.next = head.next; : result.random = result.random; : return result; : } : result是一个新的node 然后result的next连null, result.random指向result.random : 是什么意思?
|
a**********0 发帖数: 422 | 5 /**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head == null)
return null;
if(head.next == null){
RandomListNode result = new RandomListNode(head.label);
result.next = null;
if(head.random != null)
result.random = result;
return result;
}
RandomListNode scanner = head;
while(scanner != null){
RandomListNode temp = new RandomListNode(scanner.label);
temp.next = scanner.next;
scanner.next = temp;
scanner = scanner.next.next;
}
scanner = head;
while(scanner != null){
if(scanner.random != null)
scanner.next.random = scanner.random.next;
scanner = scanner.next.next;
}
RandomListNode fakeHead = head.next;
RandomListNode scannerOne = head;
RandomListNode scannerTwo = fakeHead;
while(scannerTwo.next != null){
RandomListNode temp = scannerTwo.next;
scannerTwo.next= scannerTwo.next.next;
scannerOne.next = temp;
scannerOne = scannerOne.next;
scannerTwo = scannerTwo.next;
}
scannerOne.next = null;
return fakeHead;
}
}
通过了
就是按照你说的改的
random
【在 m*********n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 对java不熟 我感觉好像有点问题比如 这段代码: : if(head.next == null){ : RandomListNode result = new RandomListNode(head.label); : result.next = head.next; : result.random = result.random; : return result; : } : result是一个新的node 然后result的next连null, result.random指向result.random : 是什么意思?
|