leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1019.cpp (503B)


      1 class Solution {
      2   public:
      3     vector<int> nextLargerNodes(ListNode *head) {
      4         if (!head) return {};
      5 
      6         int len = 0, i = 0;
      7         vector<int> res;
      8         stack<pair<int, int>> st;
      9 
     10         for (ListNode *p = head; p; p = p->next, i++) {
     11             res.push_back(0);
     12             while (!st.empty() && p->val > st.top().first) {
     13                 res[st.top().second] = p->val;
     14                 st.pop();
     15             }
     16             st.push({p->val, i});
     17         }
     18         return res;
     19     }
     20 };