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 d9e6ae3 commit 1bfabf1Copy full SHA for 1bfabf1
src/stack/Problem1019_NextGreaterNodeInLinkedList.java
@@ -0,0 +1,23 @@
1
+package stack;
2
+
3
+import data.ListNode;
4
5
+import java.util.ArrayList;
6
+import java.util.Stack;
7
8
+public class Problem1019_NextGreaterNodeInLinkedList {
9
10
+ public int[] nextLargerNodes(ListNode head) {
11
+ ArrayList<Integer> A = new ArrayList<>();
12
+ for (ListNode node = head; node != null; node = node.next)
13
+ A.add(node.val);
14
+ int[] res = new int[A.size()];
15
+ Stack<Integer> stack = new Stack<>();
16
+ for (int i = 0; i < A.size(); ++i) {
17
+ while (!stack.isEmpty() && A.get(stack.peek()) < A.get(i))
18
+ res[stack.pop()] = A.get(i);
19
+ stack.push(i);
20
+ }
21
+ return res;
22
23
+}
0 commit comments