forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1019.java
40 lines (36 loc) · 1.05 KB
/
_1019.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
public class _1019 {
public static class Solution1 {
public int[] nextLargerNodes(ListNode head) {
int len = findLength(head);
int[] result = new int[len];
ListNode tmp = head;
int i = 0;
while (tmp != null) {
result[i++] = findNextLarger(tmp.val, tmp);
tmp = tmp.next;
}
return result;
}
private int findNextLarger(int val, ListNode head) {
ListNode tmp = head.next;
while (tmp != null) {
if (tmp.val > val) {
return tmp.val;
}
tmp = tmp.next;
}
return 0;
}
private int findLength(ListNode head) {
ListNode tmp = head;
int count = 0;
while (tmp != null) {
tmp = tmp.next;
count++;
}
return count;
}
}
}