由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 各位刷友,leetcode里的题目:Copy List with Random Pointer
相关主题
copy list with random pointer 老出错copy 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?请教 Iterator 一题
请问大牛们如何提高解决leetcode上面Linkedlist的题的能力?How can one determine whether a singly linked list has a cycle?
leetcode Copy List with Random Pointer有人同看Populating Next Right Pointers in Each Node II的recursive写法么?
哪位大侠帮我看看这个coderemove a node (and its memory) from a doubly linked list
请教下copy list with random pointer面试题
求救! Copy List With Random Pointer总是超时请问怎样写没有parent pointer的BST iterator?
相关话题的讨论汇总
话题: iter话题: random话题: nhead话题: next
进入JobHunting版参与讨论
1 (共1页)
y*****3
发帖数: 451
1
test case是怎么表示的?比如这个:{-1,8,7,-3,4,4,-3,#,#,-1}是啥意思啊??
y*****3
发帖数: 451
2
急等,哭求,在线等。。有大包子伺候。。谢谢!!!
s***e
发帖数: 403
3
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
map corresponding;
corresponding[nullptr] = nullptr;

RandomListNode* nhead = new RandomListNode(0);
RandomListNode* last = nhead;
RandomListNode* iter = head;

// copy the original list
while(iter != nullptr)
{
RandomListNode* new_node = new RandomListNode(iter->label);
new_node->random = iter->random;
last->next = new_node;

corresponding[iter] = new_node;

last = last->next;
iter = iter->next;
}

// re-iterate to link the random pointer
iter = nhead -> next;
while(iter != nullptr)
{
iter->random = corresponding[iter->random];
iter = iter->next;
}

iter = nhead;
nhead = nhead->next;
delete iter;
return nhead;
}
};
u*****o
发帖数: 1224
4
mark一下
c*******2
发帖数: 60
5
貌似前半段是list, 后半段是random指针,
所以 list是 -1 8 7 -3 4
然后各节点的random指向 4 -3 # # -1
#表示空指针
1 (共1页)
进入JobHunting版参与讨论
相关主题
请问怎样写没有parent pointer的BST iterator?leetcode Copy List with Random Pointer
reverse an array哪位大侠帮我看看这个code
谁对design pattern比较熟?请教下copy list with random pointer
我今年的第一次面试,恶心坏了求救! Copy List With Random Pointer总是超时
copy list with random pointer 老出错copy 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?请教 Iterator 一题
请问大牛们如何提高解决leetcode上面Linkedlist的题的能力?How can one determine whether a singly linked list has a cycle?
相关话题的讨论汇总
话题: iter话题: random话题: nhead话题: next