File tree 5 files changed +250
-0
lines changed
lcof2/剑指 Offer II 028. 展平多级双向链表
solution/0400-0499/0430.Flatten a Multilevel Doubly Linked List
5 files changed +250
-0
lines changed Original file line number Diff line number Diff line change @@ -187,6 +187,58 @@ class Solution {
187
187
}
188
188
```
189
189
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
+
190
242
### ** ...**
191
243
192
244
```
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -169,6 +169,58 @@ class Solution {
169
169
}
170
170
```
171
171
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
+
172
224
### ** ...**
173
225
174
226
```
Original file line number Diff line number Diff line change @@ -166,6 +166,58 @@ class Solution {
166
166
}
167
167
```
168
168
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
+
169
221
### ** ...**
170
222
171
223
```
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments