-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0138.cpp
More file actions
102 lines (91 loc) · 1.92 KB
/
0138.cpp
File metadata and controls
102 lines (91 loc) · 1.92 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node *copyRandomList(Node *head) {
// return func1(head);
return func2(head);
}
Node *func1(Node *head) {
if (head == NULL) {
return NULL;
}
Node *head1 = head;
Node *curr1 = head1;
Node *head2 = new Node(head1->val);
Node *curr2 = head2;
map<Node *, int> map1;
map<int, Node *> map2;
int index = 0;
map1[curr1] = index;
map2[index] = curr2;
curr1 = curr1->next;
while (curr1) {
index++;
map1[curr1] = index;
Node *node = new Node(curr1->val);
curr2->next = node;
curr2 = curr2->next;
map2[index] = curr2;
curr1 = curr1->next;
}
curr1 = head1;
curr2 = head2;
while (curr1) {
if (curr1->random) {
curr2->random = map2[map1[curr1->random]];
}
curr1 = curr1->next;
curr2 = curr2->next;
}
return head2;
}
Node *func2(Node *head) {
if (head == NULL) {
return NULL;
}
Node *curr = head;
while (curr != NULL) {
Node *next = curr->next;
Node *copy = new Node(curr->val);
copy->random = curr->random;
curr->next = copy;
copy->next = next;
curr = next;
}
curr = head;
while (curr != NULL) {
if (curr->next->random != NULL) {
curr->next->random = curr->next->random->next;
}
curr = curr->next->next;
}
Node *head1 = head;
Node *head2 = head->next;
Node *curr1 = head1;
Node *curr2 = head2;
while (curr1 != NULL) {
curr1->next = curr2->next;
if (curr2->next != NULL) {
curr2->next = curr2->next->next;
} else {
curr2->next = NULL;
}
curr1 = curr1->next;
curr2 = curr2->next;
}
return head2;
}
};