-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1019. Next Greater Node In Linked List
77 lines (56 loc) · 1.98 KB
/
1019. Next Greater Node In Linked List
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/** 1019. Next Greater Node In Linked List
Runtime: 14 ms, faster than 75.20% of Java online submissions for Next Greater Node In Linked List.
Memory Usage: 43.5 MB, less than 47.19% of Java online submissions for Next Greater Node In Linked List.
*/
class Solution {
public int[] nextLargerNodes(ListNode head) {
ArrayList<Integer> A = new ArrayList<>();
for (ListNode node = head; node != null; node = node.next)
A.add(node.val);
int[] res = new int[A.size()];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < A.size(); i++)
{
while ( !stack.isEmpty() && A.get(stack.peek()) < A.get(i) )
res[stack.pop()] = A.get(i);
stack.push(i);
}
return res;
}
}
// public int[] nextLargerNodes(ListNode head) {
// int count=0;
// ListNode pun=head;
// ListNode res=head;
// while(pun!=null)
// {
// pun=pun.next;
// count++;
// }
// int[] arr=new int[count];
// for(int i=0;i<count;i++)
// {
// int max=Integer.MIN_VALUE;
// pun=head;
// int pos=i+1;
// while(pos>0 ){
// if(pun.next==null)
// return arr;
// pun=pun.next;
// pos--;
// }
// while(pun!=null)
// {
// if(pun.val>res.val){
// max=pun.val;
// break;
// }
// pun=pun.next;
// }
// if(res.val<max)
// arr[i]=max;
// //arr[i]=max;
// res=res.next;
// }
// return arr;
// }