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

<p>Remove every node which has a node with a greater value anywhere to the right side of it.</p>

<p>Return <em>the </em><code>head</code><em> of the modified linked list.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/02/drawio.png" style="width: 631px; height: 51px;" />
<pre>
<strong>Input:</strong> head = [5,2,13,3,8]
<strong>Output:</strong> [13,8]
<strong>Explanation:</strong> The nodes that should be removed are 5, 2 and 3.
- Node 13 is to the right of node 5.
- Node 13 is to the right of node 2.
- Node 8 is to the right of node 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> head = [1,1,1,1]
<strong>Output:</strong> [1,1,1,1]
<strong>Explanation:</strong> Every node has value 1, so no nodes are removed.
</pre>

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

<ul>
	<li>The number of the nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

 /*
1. Store the pointers in vector on purpose for simplicity.

2. Traverse vector from right to left. This will help you identify nodes

to the right larger than ones at the left.

As you traverse to the left keep track of the largest node found so far (max node value)

If a node is smaller than a node to the right do not prepend it to the new vector

Link all nodes in vector and return head node. :)
 */
class Solution {
public:
    ListNode* removeNodes(ListNode* head) {

        vector<ListNode*> arr;

        for ( ListNode * r = head ; r != NULL ; r = r->next )
        {
            arr.push_back(r);
           
        }

        const size_t ans_size = arr.size();
  
        ListNode * ans[ans_size];

        memset(ans,0x00,ans_size*sizeof(ListNode*));

        size_t k = ans_size;

        int maxval = 0;

        for ( size_t i = arr.size() - 1 , j = 0 ; j < arr.size() ; i-- , j++ )
        {
            if ( arr[i]->val >= maxval ) 
            {
                maxval = arr[i]->val;

                ans[--k] = arr[i];
            }

        }

        printf("%lu\n",k);

        for ( size_t i = k, j = k + 1 ; j < ans_size ; i++,j++ )
        {
            ans[i]->next = ans[j];
        }

        printf("Made it");

        // ans[ans_size-1]->next = NULL;

        return ans[k];
        
    }
};
185393