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 | | c*******2 发帖数: 60 | 5 貌似前半段是list, 后半段是random指针,
所以 list是 -1 8 7 -3 4
然后各节点的random指向 4 -3 # # -1
#表示空指针 |
|