File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Data Structures/Linked List Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments