/*
* @lc app=leetcode id=1019 lang=cpp
*
* [1019] Next Greater Node In Linked List
*/
// @lc code=start
/**
* 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:
vector<int> nextLargerNodes(ListNode* head) {
vector<int> V;
for(auto p = head; p != nullptr; p=p->next) V.push_back(p->val);
stack<int> s;
int t;
for(int i=V.size()-1;i>=0;i--){
t = V[i];
while( s.empty() == false && s.top() <= V[i]) s.pop();
if(s.empty()) V[i] = 0;
else V[i] = s.top();
s.push(t);
}
return V;
}
};
// @lc code=end
No.280 - LeetCode[1019] Next Greater Node In Linked List - 单调栈
最新推荐文章于 2024-04-26 09:02:35 发布