这道题就是找自己大的那个数就好 简单的解法
class Solution { public int[] nextLargerNodes(ListNode head) { int arr[] = new int[100001]; int flag = 0; while (head != null) { arr[flag++] = head.val; head = head.next; } int res[] = new int[flag]; for (int i = 0; i < flag; i++) { for (int k = i + 1; k < flag; k++) { if (arr[k] > arr[i]) { res[i] = arr[k]; break; } } } return res; } }
后面增加用栈解的方法
class Solution { public int[] nextLargerNodes(ListNode head) { ArrayList al = new ArrayList(); while(head!=null) {al.add(head.val); head = head.next; } int res[] = new int [al.size()]; Stack s = new Stack(); for (int i = 0; i < al.size(); ++i) { while (!s.isEmpty() && al.get(s.peek()) < al.get(i)) res[s.pop()] = al.get(i); s.push(i); } return res; } }