-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflatten a ll.cpp
192 lines (128 loc) Β· 3.31 KB
/
flatten a ll.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
//////////////////////////////////////////////////////
//Question/Info
Flattening a Linked List
Medium Accuracy: 33.91% Submissions: 60915 Points: 4
Given a Linked List of size N, where every node represents a sub-linked-list and contains two pointers:
(i) a next pointer to the next node,
(ii) a bottom pointer to a linked list where this node is head.
Each of the sub-linked-list is in sorted order.
Flatten the Link List such that all the nodes appear in a single level while maintaining the sorted order.
Note: The flattened list will be printed using the bottom pointer instead of next pointer.
Example 1:
Input:
5 -> 10 -> 19 -> 28
| | | |
7 20 22 35
| | |
8 50 40
| |
30 45
Output: 5-> 7-> 8- > 10 -> 19-> 20->
22-> 28-> 30-> 35-> 40-> 45-> 50.
Explanation:
The resultant linked lists has every
node in a single level.
(Note: | represents the bottom pointer.)
Example 2:
Input:
5 -> 10 -> 19 -> 28
| |
7 22
| |
8 50
|
30
Output: 5->7->8->10->19->20->22->30->50
Explanation:
The resultant linked lists has every
node in a single level.
(Note: | represents the bottom pointer.)
Your Task:
You do not need to read input or print anything. Complete the function flatten() that takes the head of the linked list as input parameter and returns the head of flattened link list.
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(1)
Constraints:
0 <= N <= 50
1 <= Mi <= 20
1 <= Element of linked list <= 103
Company Tags
24*7 Innovation Labs Amazon Drishti-Soft Flipkart Goldman Sachs Microsoft Paytm Payu Qualcomm Snapdeal Visa
author: srj_v
//////////////////////////////////////////////////////
*/
#include <bits/stdc++.h>
using namespace std;
#define _IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long double ld;
typedef long long int lli;
#pragma GCC optimize("Ofast")
void c_p_c()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////////////////////
int32_t main() {
///////////
// c_p_c();
///////////
_IOS
//////////
// code
/*
int t ; cin >> t; while(t--){}
*/
/* Node structure used in the program
struct Node{
int data;
struct Node * next;
struct Node * bottom;
Node(int x){
data = x;
next = NULL;
bottom = NULL;
}
};
*/
/* Function which returns the root of
the flattened linked list. */
Node* merge(Node * l1, Node * l2)
{
Node*temp = new Node(0);
Node*ans = temp;
while (l1 and l2) {
if (l1->data < l2->data) {
temp->bottom = l1;
temp = temp->bottom;
l1 = l1->bottom;
//NOTE...WE ARE MOVING TO BOTTOM ... NOT NEXT POINTER....
}
else {
temp->bottom = l2;
temp = temp->bottom;
l2 = l2->bottom;
}
}
// now if any left...
if (l1) temp->bottom = l1;
else temp->bottom = l2;
return ans->bottom;
}
Node *flatten(Node * root)
{
// Your code here
// base case...
if (root == 0 or root->next == 0 ) return root;
// recursion...for list on right...
root->next = flatten(root->next);
// now merging...
root = merge(root, root->next);
return root;
}
// cerr << "time: " << clock() << " ms" << '\n';
return 0;
}
//////////////////////////////////////////////////////