由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - copy list with random pointer 老出错
相关主题
各位刷友,leetcode里的题目:Copy List with Random Pointercopy link with random additional pointers
Leetcode新题 Copy List with Random Pointerreverse random pointers of a single linked list
Leetcode Copy List with Random Pointer Runtime Error?问到linked list 的题目
leetcode Copy List with Random Pointerremove a node (and its memory) from a doubly linked list
哪位大侠帮我看看这个codereorder list 递归方法超时
请教下copy list with random pointer关于reorder list 的总结
求救! Copy List With Random Pointer总是超时How can one determine whether a singly linked list has a cycle?
请问大牛们如何提高解决leetcode上面Linkedlist的题的能力?bloomberg 电话面试问题
相关话题的讨论汇总
话题: null话题: scanner话题: fakehead话题: head
进入JobHunting版参与讨论
1 (共1页)
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 的大作中提到】
: 对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 的大作中提到】
: 对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 的大作中提到】
: 对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
: 是什么意思?

1 (共1页)
进入JobHunting版参与讨论
相关主题
bloomberg 电话面试问题哪位大侠帮我看看这个code
什么情况下pass by reference比pass by pointer好?请教下copy list with random pointer
linklist exercise求救! Copy List With Random Pointer总是超时
问个C的基本问题请问大牛们如何提高解决leetcode上面Linkedlist的题的能力?
各位刷友,leetcode里的题目:Copy List with Random Pointercopy link with random additional pointers
Leetcode新题 Copy List with Random Pointerreverse random pointers of a single linked list
Leetcode Copy List with Random Pointer Runtime Error?问到linked list 的题目
leetcode Copy List with Random Pointerremove a node (and its memory) from a doubly linked list
相关话题的讨论汇总
话题: null话题: scanner话题: fakehead话题: head