-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path430.cpp
More file actions
38 lines (37 loc) · 816 Bytes
/
430.cpp
File metadata and controls
38 lines (37 loc) · 816 Bytes
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
/*
// Definition for a Node.
class Node {
public:
int val;
Node* prev;
Node* next;
Node* child;
};
*/
class Solution {
public:
Node* flatten(Node* head) {
if(!head)
return NULL;
Node* res = head;
while(head){
if(head->child != NULL){
Node* tmp = head->next;
Node* tmp2 = flatten(head->child);
head->next = tmp2;
head->child = NULL;
tmp2->prev = head;
while(tmp2->next)
tmp2 = tmp2->next;
tmp2->next = tmp;
if(tmp)
tmp->prev = tmp2;
head = tmp;
}
else{
head = head->next;
}
}
return res;
}
};