-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
37 lines (33 loc) · 1.16 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 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)
{
if(head == NULL)
return NULL;
map<RandomListNode*, RandomListNode*> mp;
RandomListNode *newList = new RandomListNode(head->label);
newList->random = head->random;
mp.insert(map<RandomListNode*, RandomListNode*>::value_type(head,newList));
for(RandomListNode *node = head, *temp = newList; node->next != NULL; node = node->next,temp = temp->next)
{
temp->next = new RandomListNode(node->next->label);
temp->next->random = node->next->random;
mp.insert(map<RandomListNode*, RandomListNode*>::value_type(node->next,temp->next));
}
for(RandomListNode *node = newList;node != NULL;node = node->next)
{
if(node->random != NULL)
node->random = mp.find(node->random)->second;
}
return newList;
}
};