---
# System prepended metadata

title: 1019. Next Greater Node In Linked List
tags: [Leetcode, Monotonic Stack, Medium]

---

# 1019. Next Greater Node In Linked List
###### tags: `Leetcode` `Medium` `Monotonic Stack`
Link: https://leetcode.com/problems/next-greater-node-in-linked-list/
## 思路
典型找next larger题
单调递减栈
## Code
```python=
class Solution:
    def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]:
        ans, stack = [], []
        while head:
            while stack and stack[-1][1]<head.val:
                ans[stack.pop()[0]] = head.val
            stack.append([len(ans), head.val])
            ans.append(0)
            head = head.next
        return ans
```
```java=
class Solution {
    public int[] nextLargerNodes(ListNode head) {
        Stack<Integer> stack = new Stack<>();
        List<Integer> num = new ArrayList<>();
       while(head!=null){
            num.add(head.val);
            head = head.next;
        }
        int[] ans = new int[num.size()];
        for(int i=0; i<num.size(); i++){
            while(!stack.isEmpty() && num.get(stack.peek())<num.get(i)){
                int curr = stack.pop();
                ans[curr] = num.get(i);
            }
            stack.add(i);
        }
        return ans;
    }
}
```