We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ec794e3 commit 95d6b4dCopy full SHA for 95d6b4d
C++/next-greater-node-in-linked-list.cpp
@@ -0,0 +1,27 @@
1
+// Time: O(n)
2
+// Space: O(n)
3
+
4
+/**
5
+ * Definition for singly-linked list.
6
+ * struct ListNode {
7
+ * int val;
8
+ * ListNode *next;
9
+ * ListNode(int x) : val(x), next(NULL) {}
10
+ * };
11
+ */
12
+class Solution {
13
+public:
14
+ vector<int> nextLargerNodes(ListNode* head) {
15
+ vector<int> result;
16
+ vector<pair<int, int>> stk;
17
+ for (auto node = head; node; node = node->next) {
18
+ while (!stk.empty() && stk.back().second < node->val) {
19
+ result[stk.back().first] = node->val;
20
+ stk.pop_back();
21
+ }
22
+ stk.emplace_back(result.size(), node->val);
23
+ result.emplace_back(0);
24
25
+ return result;
26
27
+};
0 commit comments