-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyRandomList.cpp
More file actions
39 lines (35 loc) · 1.06 KB
/
copyRandomList.cpp
File metadata and controls
39 lines (35 loc) · 1.06 KB
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
38
39
#include "copyRandomList.h"
#include <cstddef>
#include <unordered_map>
#include <iostream>
using namespace std;
RandomListNode* Solution::copyRandomList(RandomListNode* head) {
if(head == 0)
return NULL;
unordered_map<RandomListNode*, RandomListNode*> map;
//map.insert(make_pair(NULL, NULL));
RandomListNode* newHead = new RandomListNode(head->label);
map.insert(make_pair((RandomListNode*) head, newHead));
RandomListNode* current = head->next;
RandomListNode* newCurrentPre = newHead;
while(current) {
cout<<"In the copying preces\n";
RandomListNode* tmp = new RandomListNode(current->label);
newCurrentPre->next = tmp;
map.insert(make_pair(current, tmp));
current = current->next;
newCurrentPre = newCurrentPre->next;
}
current = head;
RandomListNode* newCurrent = newHead;
while( current && newCurrent) {
cout<<"Setting the random Porinter\n";
if(current->random)
newCurrent->random = map[current->random];
else
newCurrent->random = NULL;
current = current->next;
newCurrent = newCurrent->next;
}
return newHead;
}