Skip to content

Commit 5addf79

Browse files
committed
430. Flatten a Multilevel Doubly Linked List
1 parent 07b70d9 commit 5addf79

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/
2+
3+
class Solution {
4+
public:
5+
Node* flatten(Node* head) {
6+
flattenList(head);
7+
return head;
8+
}
9+
Node* flattenList(Node* head){
10+
if(head==NULL) return NULL;
11+
Node* temp = head;
12+
Node* curr = temp;
13+
14+
if(temp->child!=NULL){
15+
Node* remain = temp->next;
16+
temp->next = temp->child;
17+
temp->child->prev = temp;
18+
temp->child = NULL;
19+
curr = flattenList(temp->next);
20+
if(remain!=NULL){
21+
curr->next = remain;
22+
remain->prev = curr;
23+
return flattenList(remain);
24+
}
25+
return curr;
26+
}
27+
else if(temp->next!=NULL){
28+
return flattenList(temp->next);
29+
}
30+
else {
31+
return temp;
32+
}
33+
}
34+
};

0 commit comments

Comments
 (0)