There was an error while loading. Please reload this page.
1 parent acedf29 commit 5fed58cCopy full SHA for 5fed58c
Stack/1019.Next-Greater-Node-In-Linked-List/1019.Next-Greater-Node-In-Linked-List.cpp
@@ -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