-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelete nodes having greater value on right.cpp
281 lines (230 loc) · 4.65 KB
/
Delete nodes having greater value on right.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next;
Node(int x)
{
data = x;
next = NULL;
}
};
void print(Node *root)
{
Node *temp = root;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
// } Driver Code Ends
/*
The structure of linked list is the following
struct Node
{
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
}
};
*/
/*
Delete nodes having greater value on right
Input:
1
8
12 15 10 11 5 6 2 3
Output:
15 11 6 3
12 15 10 11 5 6 2 3
Explanation of First Approach:
First iteration until the condition is satisfied
curr 12 15 10 11 5 6 2 3
head 12 15 10 11 5 6 2 3
curr 15 15 10 11 5 6 2 3
head 15 15 10 11 5 6 2 3
curr 15 10 11 5 6 2 3
head 15 10 11 5 6 2 3
curr 15 10 11 5 6 2 3
curr 10 11 5 6 2 3
Second iteration until the condition is satisfied
curr 10 11 5 6 2 3
head 15 10 11 5 6 2 3
curr 11 11 5 6 2 3
head 15 11 11 5 6 2 3
curr 11 5 6 2 3
head 15 11 5 6 2 3
curr 15 11 5 6 2 3
curr 5 6 2 3
Third iteration until the condition is satisfied
curr 5 6 2 3
head 15 11 5 6 2 3
curr 6 6 2 3
head 15 11 6 6 2 3
curr 6 2 3
head 15 11 6 2 3
curr 15 11 6 2 3
curr 2 3
Fourth iteration until the condition is satisfied
curr 2 3
head 15 11 6 2 3
curr 3 3
head 15 11 6 3 3
curr 3
head 15 11 6 3
curr 15 11 6 3
Explanation of Second approach:
Time Complexity O(N)
head 12 15 10 11 5 6 2 3
curr 12 15 10 11 5 6 2 3
prev N
next N
After Reverse
head 3 2 6 5 11 10 15 12
prev 3 2 6 5 11 10 15 12
curr N
next N
current 3 2 6 5 11 10 15 12
maxNode 3 2 6 5 11 10 15 12
First Iteration 2 < 3
temp 2 6 5 11 10 15 12
current 3 6 5 11 10 15 12
maxNode 3 6 5 11 10 15 12
head 3 6 5 11 10 15 12
Second Iteration 6 < 3 false
current 6 5 11 10 15 12
maxNode 6 5 11 10 15 12
head 3 6 5 11 10 15 12
Third Iteration 5 < 6
temp 5 11 10 15 12
current 6 11 10 15 12
maxNode 6 11 10 15 12
head 3 6 11 10 15 12
Fourth Iteration 11 < 6 false
current 11 10 15 12
maxNode 11 10 15 12
head 3 6 11 10 15 12
Fifth Iteration 10 < 11
temp 10 15 12
current 11 15 12
maxNode 11 15 12
head 3 6 11 15 12
Sixth Iteration 15 < 11 false
current 15 12
maxNode 15 12
head 3 6 11 15 12
Seventh Iteration 12 < 15
temp 12
current 15
maxNode 15
head 3 6 11 15
curr 3 6 11 15
prev N
next N
current 15
maxNode 15
head 3 6 11 15
After Reverse
curr N
next N
prev 15 11 6 3
current 15 11 6 3
maxNode 15 11 6 3
head 15 11 6 3
*/
class Solution
{
public:
// Node *compute(Node *head)
// {
// Node *curr = head;
// while (curr->next)
// {
// if (curr->data < curr->next->data)
// {
// curr->data = curr->next->data;
// Node *temp = curr->next;
// curr->next = curr->next->next;
// delete temp;
// curr = head;
// }
// else
// {
// curr = curr->next;
// }
// }
// return head;
// }
Node *Reverse(Node *head)
{
Node *cur = head;
Node *prev = NULL;
Node *next;
while (cur != NULL)
{
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
head = prev;
return head;
}
Node *compute(Node *head)
{
head = Reverse(head);
Node *current = head;
Node *maxNode = head;
Node *temp;
while (current != NULL && current->next != NULL)
{
if (current->next->data < maxNode->data)
{
temp = current->next;
current->next = temp->next;
free(temp);
}
else
{
current = current->next;
maxNode = current;
}
}
return Reverse(head);
}
};
//{ Driver Code Starts.
int main()
{
int T;
cin >> T;
while (T--)
{
int K;
cin >> K;
struct Node *head = NULL;
struct Node *temp = head;
for (int i = 0; i < K; i++)
{
int data;
cin >> data;
if (head == NULL)
head = temp = new Node(data);
else
{
temp->next = new Node(data);
temp = temp->next;
}
}
Solution ob;
Node *result = ob.compute(head);
print(result);
cout << endl;
}
}
// } Driver Code Ends