-
Notifications
You must be signed in to change notification settings - Fork 1
/
1019-NextGreaterNodeInLinkedList.java
52 lines (43 loc) · 1.28 KB
/
1019-NextGreaterNodeInLinkedList.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//Question Link - https://leetcode.com/problems/next-greater-node-in-linked-list/
/**
* 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 lengthOfList(ListNode head){
ListNode temp = head;
int len = 0;
while(temp != null){
temp = temp.next;
len++;
}
return len;
}
public int[] nextLargerNodes(ListNode head) {
//get len of list
int len = lengthOfList(head);
List<Integer> val = new ArrayList<>();
int[] ans = new int[len];
//copy values of list to val[]
while (head != null) {
val.add(head.val);
head = head.next;
}
//using stack to update next greater element
Stack<Integer> s = new Stack<>();
for (int i = 0; i < len; ++i) {
while (!s.isEmpty() && val.get(s.peek()) < val.get(i)) {
ans[s.peek()] = val.get(i);
s.pop();
}
s.add(i);
}
return ans;
}
}