Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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> stack, res;
- ListNode* tmp;
- tmp = head;
- while(head){
- while(stack.size() && res[stack.back()] < head->val){
- res[stack.back()] = head->val;
- stack.pop_back();
- }
- stack.push_back(res.size());
- res.push_back(head->val);
- head = head->next;
- }
- for(int i=0; i<stack.size(); i++)
- res[stack[i]] = 0;
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement