leetcode_2487
. - 力扣(LeetCode)
这个也是一个单调的答案 也是可以极端假设 先把所有元素边遍历 边加入到栈当中 条件不符 在进行弹栈
/*** Definition for singly-linked list.* 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* removeNodes(ListNode* head) {vector< ListNode* > vec;ListNode* yummy = new ListNode(100000, head);ListNode* curr = head;vec.push_back( yummy );while(curr){while ( vec.back()->val < curr->val ){vec.pop_back();}vec.push_back( curr );curr = curr->next;}for (int i = 0; i < vec.size() - 1; ++i){vec[i]->next =vec[i + 1];}vec[ vec.size() - 1]->next = NULL;return vec[0]->next;}
};