Skip to content

Commit 5fed58c

Browse files
authored
Create 1019.Next-Greater-Node-In-Linked-List.cpp
1 parent acedf29 commit 5fed58c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
vector<int> nextLargerNodes(ListNode* head)
12+
{
13+
vector<int>ret;
14+
stack<pair<int,int>>Stack;
15+
ListNode* p = head;
16+
int i = 0;
17+
18+
while (p!=NULL)
19+
{
20+
while (!Stack.empty() && Stack.top().first < p->val)
21+
{
22+
ret[Stack.top().second] = p->val;
23+
Stack.pop();
24+
}
25+
ret.push_back({0});
26+
Stack.push({p->val, i});
27+
i++;
28+
p = p->next;
29+
}
30+
31+
return ret;
32+
}
33+
};

0 commit comments

Comments
 (0)