-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2487.cpp
More file actions
70 lines (59 loc) · 1.62 KB
/
Copy path2487.cpp
File metadata and controls
70 lines (59 loc) · 1.62 KB
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
#include <vector>
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) {}
};
class Solution {
public:
ListNode* reverse(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
while (curr) {
ListNode* temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
return prev;
}
ListNode* removeNodes(ListNode* head) {
head = reverse(head);
ListNode* curr = head;
int curr_max = head->val;
while (curr && curr->next) {
if (curr->next->val < curr_max) {
curr->next = curr->next->next;
} else {
curr_max = curr->next->val;
curr = curr->next;
}
}
return reverse(head);
}
};
/* ListNode* monotonicStack(ListNode* head) {
vector<int> st;
ListNode* curr = head;
while (curr) {
while (!st.empty() && curr->val > st.back()) { st.pop_back(); };
st.push_back(curr->val);
curr = curr->next;
}
ListNode* dummy = new ListNode();
curr = dummy;
for (int num : st) {
curr->next = new ListNode(num);
curr = curr->next;
}
return dummy->next;
} */
/* ListNode* recursion(ListNode* head) {
if (!head) return nullptr;
head->next = removeNodes(head->next);
if (head->next && head->val < head->next->val) { return head->next; }
return head;
} */