-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_1019.java
More file actions
33 lines (30 loc) · 888 Bytes
/
Copy pathleetCode_1019.java
File metadata and controls
33 lines (30 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public int[] nextLargerNodes(ListNode head) {
int length = 0;
ListNode temp = head;
List<Integer> list = new ArrayList<Integer>();
while(temp != null){
list.add(temp.val);
temp = temp.next;
}
int arr[] = new int[list.size()];
Stack<Integer> stack = new Stack<>();
for(int i=0;i<list.size();i++){
while(!stack.isEmpty() && list.get(stack.peek()) < list.get(i)){
arr[stack.pop()] = list.get(i);
}
stack.push(i);
}
return arr;
}
}