-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloneSiblingList.cpp
More file actions
130 lines (93 loc) · 2.83 KB
/
CloneSiblingList.cpp
File metadata and controls
130 lines (93 loc) · 2.83 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <vector>
#include <iostream>
#include <unordered_map>
#include <cassert>
using namespace std;
struct ComplexListNode {
ComplexListNode(void): val(0), next(nullptr), sibling(nullptr) {}
ComplexListNode(int _val): val(_val), next(nullptr), sibling(nullptr) {}
int val;
ComplexListNode *next;
ComplexListNode *sibling;
};
void PrintList( ComplexListNode *head);
void CloneNodes( ComplexListNode *head) {
ComplexListNode *p = head;
while ( p != nullptr) {
ComplexListNode *tmp = new ComplexListNode(p->val);
tmp->next = p->next;
p->next = tmp;
p = tmp->next;
}
return;
}
void ConnectSiblingNode( ComplexListNode *head) {
ComplexListNode *p = head;
while( p != nullptr) {
if( p->sibling) p->next->sibling = p->sibling->next;
p = p->next->next;
}
return;
}
ComplexListNode* SplitCopiedList( ComplexListNode *head) {
ComplexListNode *copied_head = head->next;
ComplexListNode *p = head->next;
while( p != nullptr) {
ComplexListNode *tmp = p->next;
if( p->next) p->next = p->next->next;
p = p->next;
if( tmp) delete tmp;
}
if( head) delete head;
return copied_head;
}
ComplexListNode* CloneListWithComplexNode( ComplexListNode *head) {
CloneNodes(head);
//PrintList(head);
ConnectSiblingNode(head);
//PrintList(head);
return SplitCopiedList(head);
}
void PrintList( ComplexListNode *head) {
cout << "==========begin============" << endl;
for( auto p = head; p != nullptr; p = p->next) {
cout << "p->val: " << p->val;
if( p->sibling) cout << " p->sibling->val: " << p->sibling->val << endl;
else cout << endl;
}
cout << "===========end=============" << endl;
}
int main(void) {
ComplexListNode *node1 = new ComplexListNode(1);
ComplexListNode *node2 = new ComplexListNode(2);
ComplexListNode *node3 = new ComplexListNode(3);
ComplexListNode *node4 = new ComplexListNode(4);
ComplexListNode *node5 = new ComplexListNode(5);
ComplexListNode *node6 = new ComplexListNode(6);
ComplexListNode *node7 = new ComplexListNode(7);
ComplexListNode *node8 = new ComplexListNode(8);
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
node5->next = node6;
node6->next = node7;
node7->next = node8;
node3->sibling = node4;
node5->sibling = node1;
node1->sibling = node8;
node8->sibling = node1;
PrintList(node1);
ComplexListNode *copy_root;
copy_root = CloneListWithComplexNode(node1);
assert(copy_root->sibling->val == 8);
assert(copy_root->next->next->sibling->val == 4);
assert(copy_root->next->next->next->next->sibling->val == 1);
assert(copy_root->sibling->sibling->val == 1);
for( auto p = copy_root; p != nullptr;) {
ComplexListNode *tmp = p;
p=p->next;
delete tmp;
}
return 0;
}