Skip to content

Commit a7db496

Browse files
authored
feat: add solutions to lc/lcof2 problems (#638)
lcof2 No.028 & lc No.0430. Flatten a Multilevel Doubly Linked List
1 parent 2297fda commit a7db496

File tree

5 files changed

+250
-0
lines changed

5 files changed

+250
-0
lines changed

lcof2/剑指 Offer II 028. 展平多级双向链表/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,58 @@ class Solution {
187187
}
188188
```
189189

190+
### **C++**
191+
192+
```cpp
193+
/*
194+
// Definition for a Node.
195+
class Node {
196+
public:
197+
int val;
198+
Node* prev;
199+
Node* next;
200+
Node* child;
201+
};
202+
*/
203+
204+
class Solution {
205+
public:
206+
Node* flatten(Node* head) {
207+
flattenGetTail(head);
208+
return head;
209+
}
210+
211+
Node* flattenGetTail(Node* head) {
212+
Node* cur = head;
213+
Node* tail = nullptr;
214+
215+
while (cur) {
216+
Node* next = cur->next;
217+
if (cur->child) {
218+
Node* child = cur->child;
219+
Node* childTail = flattenGetTail(cur->child);
220+
221+
cur->child = nullptr;
222+
cur->next = child;
223+
child->prev = cur;
224+
childTail->next = next;
225+
226+
if (next)
227+
next->prev = childTail;
228+
229+
tail = childTail;
230+
} else {
231+
tail = cur;
232+
}
233+
234+
cur = next;
235+
}
236+
237+
return tail;
238+
}
239+
};
240+
```
241+
190242
### **...**
191243

192244
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* prev;
7+
Node* next;
8+
Node* child;
9+
};
10+
*/
11+
12+
class Solution {
13+
public:
14+
Node* flatten(Node* head) {
15+
flattenGetTail(head);
16+
return head;
17+
}
18+
19+
Node* flattenGetTail(Node* head) {
20+
Node* cur = head;
21+
Node* tail = nullptr;
22+
23+
while (cur) {
24+
Node* next = cur->next;
25+
if (cur->child) {
26+
Node* child = cur->child;
27+
Node* childTail = flattenGetTail(cur->child);
28+
29+
cur->child = nullptr;
30+
cur->next = child;
31+
child->prev = cur;
32+
childTail->next = next;
33+
34+
if (next)
35+
next->prev = childTail;
36+
37+
tail = childTail;
38+
} else {
39+
tail = cur;
40+
}
41+
42+
cur = next;
43+
}
44+
45+
return tail;
46+
}
47+
};

solution/0400-0499/0430.Flatten a Multilevel Doubly Linked List/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,58 @@ class Solution {
169169
}
170170
```
171171

172+
### **C++**
173+
174+
```cpp
175+
/*
176+
// Definition for a Node.
177+
class Node {
178+
public:
179+
int val;
180+
Node* prev;
181+
Node* next;
182+
Node* child;
183+
};
184+
*/
185+
186+
class Solution {
187+
public:
188+
Node* flatten(Node* head) {
189+
flattenGetTail(head);
190+
return head;
191+
}
192+
193+
Node* flattenGetTail(Node* head) {
194+
Node* cur = head;
195+
Node* tail = nullptr;
196+
197+
while (cur) {
198+
Node* next = cur->next;
199+
if (cur->child) {
200+
Node* child = cur->child;
201+
Node* childTail = flattenGetTail(cur->child);
202+
203+
cur->child = nullptr;
204+
cur->next = child;
205+
child->prev = cur;
206+
childTail->next = next;
207+
208+
if (next)
209+
next->prev = childTail;
210+
211+
tail = childTail;
212+
} else {
213+
tail = cur;
214+
}
215+
216+
cur = next;
217+
}
218+
219+
return tail;
220+
}
221+
};
222+
```
223+
172224
### **...**
173225

174226
```

solution/0400-0499/0430.Flatten a Multilevel Doubly Linked List/README_EN.md

+52
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,58 @@ class Solution {
166166
}
167167
```
168168

169+
### **C++**
170+
171+
```cpp
172+
/*
173+
// Definition for a Node.
174+
class Node {
175+
public:
176+
int val;
177+
Node* prev;
178+
Node* next;
179+
Node* child;
180+
};
181+
*/
182+
183+
class Solution {
184+
public:
185+
Node* flatten(Node* head) {
186+
flattenGetTail(head);
187+
return head;
188+
}
189+
190+
Node* flattenGetTail(Node* head) {
191+
Node* cur = head;
192+
Node* tail = nullptr;
193+
194+
while (cur) {
195+
Node* next = cur->next;
196+
if (cur->child) {
197+
Node* child = cur->child;
198+
Node* childTail = flattenGetTail(cur->child);
199+
200+
cur->child = nullptr;
201+
cur->next = child;
202+
child->prev = cur;
203+
childTail->next = next;
204+
205+
if (next)
206+
next->prev = childTail;
207+
208+
tail = childTail;
209+
} else {
210+
tail = cur;
211+
}
212+
213+
cur = next;
214+
}
215+
216+
return tail;
217+
}
218+
};
219+
```
220+
169221
### **...**
170222

171223
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* prev;
7+
Node* next;
8+
Node* child;
9+
};
10+
*/
11+
12+
class Solution {
13+
public:
14+
Node* flatten(Node* head) {
15+
flattenGetTail(head);
16+
return head;
17+
}
18+
19+
Node* flattenGetTail(Node* head) {
20+
Node* cur = head;
21+
Node* tail = nullptr;
22+
23+
while (cur) {
24+
Node* next = cur->next;
25+
if (cur->child) {
26+
Node* child = cur->child;
27+
Node* childTail = flattenGetTail(cur->child);
28+
29+
cur->child = nullptr;
30+
cur->next = child;
31+
child->prev = cur;
32+
childTail->next = next;
33+
34+
if (next)
35+
next->prev = childTail;
36+
37+
tail = childTail;
38+
} else {
39+
tail = cur;
40+
}
41+
42+
cur = next;
43+
}
44+
45+
return tail;
46+
}
47+
};

0 commit comments

Comments
 (0)