forked from GDSC-IGDTUW-Autumn-of-Code-2022/dsa-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove Nodes From Linked List.cpp
61 lines (56 loc) · 1.35 KB
/
Remove Nodes From Linked List.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
#include<bits/stdc++.h>
#define ll int
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode* removeNodes(ListNode* head) {
stack<ListNode*> st;
ListNode* temp = head;
while(temp)
{
while(st.size() and temp->val>st.top()->val)
st.pop();
st.push(temp);
temp = temp->next;
}
ListNode* ans;
ans = st.top();
temp = ans;
st.pop();
while(st.size())
temp->next = st.top(), temp = st.top(), st.pop();
temp->next = nullptr;
ListNode* pre = nullptr;
while(ans)
{
ListNode* nex = ans->next;
ans->next = pre;
pre = ans;
ans = nex;
}
return pre;
}
int main()
{
ll n;
cin>>n;
vector<vector<ll>> matches(n);
for(ll i=0;i<n;i++)
{
ll a,b;
cin>>a>>b;
matches[i] = {a,b};
}
vector<vector<ll>> ans = findWinners(matches);
for(auto i:ans)
{
for(auto j:i)
cout<<j<<" ";
cout<<endl;
}
}