<p>You are given the <code>head</code> of a linked list with <code>n</code> nodes.</p>

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

<p>Return an integer array <code>answer</code> where <code>answer[i]</code> is the value of the next greater node of the <code>i<sup>th</sup></code> node (<strong>1-indexed</strong>). If the <code>i<sup>th</sup></code> node does not have a next greater node, set <code>answer[i] = 0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/05/linkedlistnext1.jpg" style="width: 304px; height: 133px;" />
<pre>
<strong>Input:</strong> head = [2,1,5]
<strong>Output:</strong> [5,5,0]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/05/linkedlistnext2.jpg" style="width: 500px; height: 113px;" />
<pre>
<strong>Input:</strong> head = [2,7,4,3,5]
<strong>Output:</strong> [7,0,5,5,0]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the list is <code>n</code>.</li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
</ul>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    // Helper function to reverse the linked list and count nodes
    ListNode* reverse(ListNode* head, int &count) {
        ListNode* curr = head;
        ListNode* prev = NULL;

        while (curr) {
            count++;  // Counting the number of nodes
            ListNode* temp = curr->next;  // Store the next node
            curr->next = prev;  // Reverse the current node's pointer
            prev = curr;  // Move prev to the current node
            curr = temp;  // Move to the next node using the stored temp
        }

        return prev;  // New head of the reversed list
    }

    // Function to find next larger nodes
    vector<int> nextLargerNodes(ListNode* head) {
        int count = 0;

        // Reverse the list and count the number of nodes
        head = reverse(head, count);

        stack<int> st;
        st.push(-1);  // Initialize the stack with -1 as a placeholder for no larger node

        vector<int> result(count);

        ListNode* curr = head;
        
        while (curr) {
            // Pop elements from the stack that are less than or equal to the current node value
            while (st.top() != -1 && st.top() <= curr->val) {
                st.pop();
            }

            if(st.top()==-1)
            result[--count] = 0 ;
            else
            result[--count] = st.top();  // Assign the next larger element from the stack
            st.push(curr->val);  // Push the current node value onto the stack
            
            curr = curr->next
            ;  // Move to the next node in the reversed list
        }

        return result;  // Return the result array
    }
};

40700