Advertisement
nikunjsoni

1019

Mar 16th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. class Solution {
  12. public:
  13.     vector<int> nextLargerNodes(ListNode* head) {
  14.         vector<int> stack, res;
  15.         ListNode* tmp;
  16.         tmp = head;
  17.         while(head){
  18.             while(stack.size() && res[stack.back()] < head->val){
  19.                 res[stack.back()] = head->val;
  20.                 stack.pop_back();
  21.             }
  22.             stack.push_back(res.size());
  23.             res.push_back(head->val);
  24.             head = head->next;
  25.         }
  26.         for(int i=0; i<stack.size(); i++)
  27.             res[stack[i]] = 0;
  28.         return res;
  29.     }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement