Skip to content

Commit ad4b75a

Browse files
authored
Create Solution.java
1 parent 1b8631f commit ad4b75a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] nextLargerNodes(ListNode head) {
3+
List<Integer> list = new ArrayList<>();
4+
while (head != null) {
5+
list.add(head.val);
6+
head = head.next;
7+
}
8+
int[] res = new int[list.size()];
9+
Deque<Integer> stack = new ArrayDeque<>();
10+
for (int i = 0; i < list.size(); ++i) {
11+
while (!stack.isEmpty() && list.get(i) > list.get(stack.peek())) {
12+
res[stack.pop()] = list.get(i);
13+
}
14+
stack.push(i);
15+
}
16+
return res;
17+
}
18+
}

0 commit comments

Comments
 (0)