<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;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
 static auto _ = []() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  return nullptr;
}();
int nums[100001];
int init= [] {
    ofstream out("user.out");
    cout.rdbuf(out.rdbuf());
    for(string s; getline(cin,s); cout << "]\n") {
        cout << "[";
        int n{};
        for(int _i = s.size() - 2; _i >=0; --_i) {
            int v = s[_i--]&15;
            int p{1};
            while((s[_i]&15) < 10) {
                p*=10;
                v += p*(s[_i--]&15);                
            }
            if (!n || nums[n - 1] <= v) {
                nums[n++] = v;
            }
        }        
        cout << nums[n - 1];
        for (int i{n - 2}; i >= 0; --i) cout << ',' << nums[i];
    }  
    exit(0);
    return 0;
}();
class Solution {
public:
    ListNode* removeNodes(ListNode* head) {
        ListNode *p=NULL,*q=head;

        while(q!=NULL){
            ListNode*temp = q->next;
            q->next=p;
            p=q;
            q=temp;
        }

        q=p->next;
        head=p;

        while(q!=NULL){
            if(p->val <= q->val){
                p=q;
                q=q->next;
            }else{
                p->next = q->next;
                q=q->next;
            }
        }
        p=head;
        q=NULL;

        while(p!=NULL){
            ListNode*temp = p->next;
            p->next=q;
            q=p;
            p=temp;
        }

        return q;
        
    }
};
10506