-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode430.cpp
More file actions
54 lines (36 loc) · 875 Bytes
/
leetcode430.cpp
File metadata and controls
54 lines (36 loc) · 875 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
// 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* curr = head;
while(curr!=NULL){
if(curr->child != NULL){
Node* next = curr->next;
curr->next =flatten(curr->child);
curr->next->prev = curr;
curr->child = NULL;
while(curr->next != NULL){
curr=curr->next;
}
if(next != NULL){
curr->next =next;
next->prev= curr;
}
}
curr=curr->next;
}
return head;
}
};