---
# System prepended metadata

title: Untitled

---

Input:  [2,7,4,3,5]
Output: [0,0,0,0,0]

List<Integer>
ListNode

Ans: [7,0,5,5,0]

Stack: [] Hold: 2

1.
Stack: [(0, 2)]  Hold: 7
!stack.isEmpty() && hold > stack.pop().val
List<Integer> or ListNode: [7,0,0,0,0]

2. 
Stack: [(1, 7)] Hold: 4 
~~!stack.isEmpty() && hold > stack.pop().val~~
List<Integer> or ListNode: [7,0,0,0,0]

3.
Stack:[(1, 7), (2, 4)] Hold: 3
~~!stack.isEmpty() && hold > stack.pop().val~~
List<Integer> or ListNode: [7,0,0,0,0]

4. 
Stack: [(1, 7), (2, 4), (3, 3)] Hold: 5
!stack.isEmpty() && hold > stack.pop().val
List<Integer> or ListNode: [7,0,0,5,0]

!stack.isEmpty() && hold > stack.pop().val
List<Integer> or ListNode: [7,0,5,5,0]

while (
!stack.isEmpty()
&&
node.val > stack.peek()
)

```java=
class Solution {
    public int[] nextLargerNodes(ListNode head) {
        List<Integer> list = new ArrayList<>();
        ListNode curr = head;
        while (curr != null) {
            list.add(curr.val);
            curr = curr.next;
        }
        curr = head;
        
        int[] ans = new int[list.size()];
        int len = list.size();
        
        for (int i = 0; i < len; i++) {
            ans[i] = compareValue(curr.val, list);
            curr = curr.next;
            list.remove(0);
        }
        
        return ans;
    }
    
    private int compareValue(int value, List<Integer> list) {
        
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) > value) {
                return list.get(i);
            }
        }
        return 0;
    }
}
```
