-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode0138.cpp
66 lines (55 loc) · 1.36 KB
/
Leetcode0138.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
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
#include <bits/stdc++.h>
using namespace std;
//Filename: Leetcode0138.cpp
//
//Author: dezhonger - csdisassembly@gmail.com
//Create: 2021-05-07 03:52:16
//Last modified: 2021-05-07 04:12:09
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) {
if (!head) return head;
Node* h = head;
while (h != NULL) {
Node* c = new Node(h -> val);
c -> next = h -> next;
h -> next = c;
h = c -> next;
}
h = head;
while (h != NULL) {
Node* p = h, *q = h -> random;
if (q == NULL) p -> next -> random = NULL;
else p -> next -> random = q -> next;
h = h -> next -> next;
}
Node* hh = head -> next, *res = hh;
h = head;
while (h != NULL) {
h -> next = hh -> next;
if (h -> next != NULL) hh -> next = h -> next -> next;
h = h -> next;
hh = hh -> next;
}
return res;
}
};
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio (false);
cin.tie (0);
return 0;
}