-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
38 lines (31 loc) · 831 Bytes
/
solution.cpp
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 == NULL) return head;
Node* current = head;
while(current != NULL) {
if(current->child == NULL) {
current = current->next;
continue;
}
Node* temp = current->child;
while(temp->next != NULL) temp = temp->next;
temp->next = current->next;
if(current->next != NULL) current->next->prev = temp;
current->next = current->child;
current->child->prev = current;
current->child = NULL;
}
return head;
}
};