力扣1019.链表中的下一个更大节点
-
从左到右
- 每个数确定下一个更大节点后 弹出
- 栈中存下标 即res.size()
-
class Solution { public: vector<int> nextLargerNodes(ListNode* head) { vector<int> res; stack<int> st; for(auto i=head;i;i=i->next) { while(!st.empty() && res[st.top()] < i->val) { res[st.top()] = i->val; st.pop(); } st.emplace(res.size()); res.push_back(i->val); } while(!st.empty()) { res[st.top()] = 0; st.pop(); } return res; } };