Skip to content

Commit

Permalink
Time: 238 ms (47.57%), Space: 43.1 MB (19.93%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Dec 17, 2022
1 parent 5e8f586 commit e06219b
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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> values;
while (head != nullptr) {
values.push_back(head->val);
head = head->next;
}

int n = int(values.size());
stack<int> iStack;
vector<int> answer(n);

for (int i = 0; i < n; ++i) {
while (!iStack.empty() && values[iStack.top()] < values[i]) {
int smaller = iStack.top();
iStack.pop();
answer[smaller] = values[i];
}
iStack.push(i);
}

return answer;
}
};

0 comments on commit e06219b

Please sign in to comment.