Skip to content

Commit 6573a81

Browse files
committed
🚀 11-Jul-2020
1 parent 1715975 commit 6573a81

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.
2+
3+
Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.
4+
5+
6+
7+
Example 1:
8+
9+
Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
10+
Output: [1,2,3,7,8,11,12,9,10,4,5,6]
11+
Explanation:
12+
13+
The multilevel linked list in the input is as follows:
14+
15+
16+
17+
After flattening the multilevel linked list it becomes:
18+
19+
20+
Example 2:
21+
22+
Input: head = [1,2,null,3]
23+
Output: [1,3,2]
24+
Explanation:
25+
26+
The input multilevel linked list is as follows:
27+
28+
1---2---NULL
29+
|
30+
3---NULL
31+
Example 3:
32+
33+
Input: head = []
34+
Output: []
35+
36+
37+
How multilevel linked list is represented in test case:
38+
39+
We use the multilevel linked list from Example 1 above:
40+
41+
1---2---3---4---5---6--NULL
42+
|
43+
7---8---9---10--NULL
44+
|
45+
11--12--NULL
46+
The serialization of each level is as follows:
47+
48+
[1,2,3,4,5,6,null]
49+
[7,8,9,10,null]
50+
[11,12,null]
51+
To serialize all levels together we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:
52+
53+
[1,2,3,4,5,6,null]
54+
[null,null,7,8,9,10,null]
55+
[null,11,12,null]
56+
Merging the serialization of each level and removing trailing nulls we obtain:
57+
58+
[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
59+
60+
61+
Constraints:
62+
63+
Number of Nodes will not exceed 1000.
64+
1 <= Node.val <= 10^5
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
/*
77+
// Definition for a Node.
78+
class Node {
79+
public:
80+
int val;
81+
Node* prev;
82+
Node* next;
83+
Node* child;
84+
};
85+
*/
86+
87+
class Solution {
88+
public:
89+
Node* flatten(Node* head) {
90+
Node *curr=head, *tail=head;
91+
stack<Node*> s;
92+
while(curr!=NULL){
93+
if(curr->child!=NULL){
94+
if(curr->next!=NULL){
95+
s.push(curr->next);
96+
curr->next->prev=NULL;
97+
}
98+
Node* child=curr->child;
99+
curr->next=child;
100+
child->prev=curr;
101+
curr->child=NULL;
102+
}
103+
tail=curr;
104+
curr=curr->next;
105+
}
106+
while(!s.empty()){
107+
curr=s.top();
108+
s.pop();
109+
tail->next=curr;
110+
curr->prev=tail;
111+
while(curr!=NULL){
112+
tail=curr;
113+
curr=curr->next;
114+
}
115+
}
116+
return head;
117+
}
118+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.
2+
3+
Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.
4+
5+
6+
7+
Example 1:
8+
9+
Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
10+
Output: [1,2,3,7,8,11,12,9,10,4,5,6]
11+
Explanation:
12+
13+
The multilevel linked list in the input is as follows:
14+
15+
16+
17+
After flattening the multilevel linked list it becomes:
18+
19+
20+
Example 2:
21+
22+
Input: head = [1,2,null,3]
23+
Output: [1,3,2]
24+
Explanation:
25+
26+
The input multilevel linked list is as follows:
27+
28+
1---2---NULL
29+
|
30+
3---NULL
31+
Example 3:
32+
33+
Input: head = []
34+
Output: []
35+
36+
37+
How multilevel linked list is represented in test case:
38+
39+
We use the multilevel linked list from Example 1 above:
40+
41+
1---2---3---4---5---6--NULL
42+
|
43+
7---8---9---10--NULL
44+
|
45+
11--12--NULL
46+
The serialization of each level is as follows:
47+
48+
[1,2,3,4,5,6,null]
49+
[7,8,9,10,null]
50+
[11,12,null]
51+
To serialize all levels together we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:
52+
53+
[1,2,3,4,5,6,null]
54+
[null,null,7,8,9,10,null]
55+
[null,11,12,null]
56+
Merging the serialization of each level and removing trailing nulls we obtain:
57+
58+
[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
59+
60+
61+
Constraints:
62+
63+
Number of Nodes will not exceed 1000.
64+
1 <= Node.val <= 10^5
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
/*
77+
// Definition for a Node.
78+
class Node {
79+
public:
80+
int val;
81+
Node* prev;
82+
Node* next;
83+
Node* child;
84+
};
85+
*/
86+
87+
class Solution {
88+
public:
89+
Node* flatten(Node* head) {
90+
Node *curr=head, *tail=head;
91+
stack<Node*> s;
92+
while(curr!=NULL){
93+
if(curr->child!=NULL){
94+
if(curr->next!=NULL){
95+
s.push(curr->next);
96+
curr->next->prev=NULL;
97+
}
98+
Node* child=curr->child;
99+
curr->next=child;
100+
child->prev=curr;
101+
curr->child=NULL;
102+
}
103+
tail=curr;
104+
curr=curr->next;
105+
}
106+
while(!s.empty()){
107+
curr=s.top();
108+
s.pop();
109+
tail->next=curr;
110+
curr->prev=tail;
111+
while(curr!=NULL){
112+
tail=curr;
113+
curr=curr->next;
114+
}
115+
}
116+
return head;
117+
}
118+
};

0 commit comments

Comments
 (0)