-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path25.cpp
More file actions
78 lines (73 loc) · 1.39 KB
/
25.cpp
File metadata and controls
78 lines (73 loc) · 1.39 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
#include <iostream>
using namespace std;
struct RandomListNode
{
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
RandomListNode() {}
};
RandomListNode* newList()
{
RandomListNode* L = new RandomListNode;
L->next = NULL;
L->random = NULL;
RandomListNode* q = L;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
RandomListNode *p = new RandomListNode;
cin >> p->label;
q->next = p;
q->random = p;
q = p;
}
q->next = NULL;
q->random = NULL;
return L->next;
}
RandomListNode* Clone(RandomListNode* pHead)
{
if (!pHead) return NULL;
RandomListNode* currNode = pHead;
while (currNode)
{
RandomListNode* node = new RandomListNode(currNode->label);
node->next = currNode->next;
currNode->next = node;
currNode = node->next;
}
currNode = pHead;
while (currNode)
{
RandomListNode* node = currNode->next;
if (currNode->random)
node->random = currNode->random->next;
currNode = node->next;
}
RandomListNode* pCloneHead = pHead->next;
RandomListNode* tmp;
currNode = pHead;
while (currNode->next)
{
tmp = currNode->next;
currNode->next = tmp->next;
currNode = tmp;
}
return pCloneHead;
}
int main()
{
ios::sync_with_stdio(false);
RandomListNode* L1 = newList();
RandomListNode* L2 = Clone(L1);
RandomListNode* p = L2;
while (p)
{
cout << p->label << " ";
p = p->next;
}
cout << endl;
return 0;
}