You are given the head of a linked list with n nodes.

For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it.

Return an integer array answer where answer[i] is the value of the next greater node of the ith node (1-indexed). If the ith node does not have a next greater node, set answer[i] = 0.

&nbsp;
Example 1:

Input: head = [2,1,5]
Output: [5,5,0]


Example 2:

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


&nbsp;
Constraints:


	The number of nodes in the list is n.
	1 &lt;= n &lt;= 104
	1 &lt;= Node.val &lt;= 109


class Solution:
    def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]:
        stack = []
        ans = []
        node = head
        
        i = 0
        while node is not None:
            while stack and stack[-1][0] < node.val:
                ans[stack[-1][1]] = node.val
                stack.pop()
            
            stack.append((node.val, i))
            ans.append(0)
            i += 1
            node = node.next
            
        return ans
class Solution {
    public int[] nextLargerNodes(ListNode head) {
        ListNode length=head;
        int l=0;
        while(length!=null)
        {
            length=length.next;
            l++;
        }
        int[] res=new int[l];
        int i=0;
        ListNode temp=head;

        while(temp!=null)
        {
            ListNode temp1=temp.next;
            int max=temp.val;

            while(temp1!=null)
            {
                if(temp1.val>max)
                {
                    max=temp1.val;
                    res[i]=max;
                    break;
                }

                temp1=temp1.next;
            }
            temp=temp.next;
            i++;
        }
        return res;
    }
}
class Solution {
public:
    vector<int> nextLargerNodes(ListNode* head) {
        vector<int> ans;
        ListNode* curr = head;
        if(head->next == NULL){
            ans.push_back(0);
            return ans;
        }

        while(curr != NULL){
            ListNode* next = curr->next;
            while(next != NULL){
                if(next->val > curr->val){
                    ans.push_back(next->val);
                    break;
                }
                next = next->next;
            }
            if(next == NULL){
                ans.push_back(0);
            }
            curr = curr->next;
        }
        return ans;
    }
};
var nextLargerNodes = function(head) {
    let arr = [];
    while(head){
        arr.push(head.val);
        head = head.next
    }
    return nextGreaterElement(arr)
    
};

function nextGreaterElement(arr){
    let temp = [];
    let n = arr.length;
    let stack = [];
    for(let i=n-1; i>=0; i--){
        while(stack.length != 0 && arr[stack[stack.length-1]] <= arr[i]){
            stack.pop()
        }
        
        if(stack.length == 0){
            temp[i] = 0
        }else{
            temp[i] = arr[stack[stack.length-1]]
        }
        stack.push(i)
    }
    return temp
}
Next Greater Node In Linked List