id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;coaching&quot;, t = &quot;coding&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Append the characters &quot;ding&quot; to the end of s so that s = &quot;coachingding&quot;. Now, t is a subsequence of s (&quot;<u><strong>co</strong></u>aching<u><strong>ding</strong></u>&quot;). It can be shown that appending any 3 characters to the end of s will never make t a subsequence. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcde&quot;, t = &quot;a&quot; <strong>Output:</strong> 0 <strong>Explanation:</strong> t is already a subsequence of s (&quot;<u><strong>a</strong></u>bcde&quot;). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;z&quot;, t = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> Append the characters &quot;abcde&quot; to the end of s so that s = &quot;zabcde&quot;. Now, t is a subsequence of s (&quot;z<u><strong>abcde</strong></u>&quot;). It can be shown that appending any 4 characters to the end of s will never make t a subsequence. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> and <code>t</code> consist only of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int appendCharacters(string s, string t) {\n int sl=s.size(),tl=t.size();\n int j=0;\n for(int i=0;i<sl&&j<tl;i++){\n if(s[i]==t[j]) j++;\n // else break;\n }\n return tl-j;\n }\n};", "memory": "12200" }
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;coaching&quot;, t = &quot;coding&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Append the characters &quot;ding&quot; to the end of s so that s = &quot;coachingding&quot;. Now, t is a subsequence of s (&quot;<u><strong>co</strong></u>aching<u><strong>ding</strong></u>&quot;). It can be shown that appending any 3 characters to the end of s will never make t a subsequence. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcde&quot;, t = &quot;a&quot; <strong>Output:</strong> 0 <strong>Explanation:</strong> t is already a subsequence of s (&quot;<u><strong>a</strong></u>bcde&quot;). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;z&quot;, t = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> Append the characters &quot;abcde&quot; to the end of s so that s = &quot;zabcde&quot;. Now, t is a subsequence of s (&quot;z<u><strong>abcde</strong></u>&quot;). It can be shown that appending any 4 characters to the end of s will never make t a subsequence. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> and <code>t</code> consist only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int appendCharacters(string s, string t) {\n int startup = 0;\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == t[startup]) startup++;\n }\n return t.length() - startup;\n }\n};", "memory": "12300" }
2,572
<p>You are given two strings <code>s</code> and <code>t</code> consisting of only lowercase English letters.</p> <p>Return <em>the minimum number of characters that need to be appended to the end of </em><code>s</code><em> so that </em><code>t</code><em> becomes a <strong>subsequence</strong> of </em><code>s</code>.</p> <p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;coaching&quot;, t = &quot;coding&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Append the characters &quot;ding&quot; to the end of s so that s = &quot;coachingding&quot;. Now, t is a subsequence of s (&quot;<u><strong>co</strong></u>aching<u><strong>ding</strong></u>&quot;). It can be shown that appending any 3 characters to the end of s will never make t a subsequence. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abcde&quot;, t = &quot;a&quot; <strong>Output:</strong> 0 <strong>Explanation:</strong> t is already a subsequence of s (&quot;<u><strong>a</strong></u>bcde&quot;). </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;z&quot;, t = &quot;abcde&quot; <strong>Output:</strong> 5 <strong>Explanation:</strong> Append the characters &quot;abcde&quot; to the end of s so that s = &quot;zabcde&quot;. Now, t is a subsequence of s (&quot;z<u><strong>abcde</strong></u>&quot;). It can be shown that appending any 4 characters to the end of s will never make t a subsequence. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> and <code>t</code> consist only of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int appendCharacters(string s, string t) {\n int i = 0;\n int j = 0;\n while(i < s.size() && j < t.size())\n {\n if(s[i] == t[j])\n {\n j++;\n }\n i++;\n \n }\n return t.size()-j;\n }\n};", "memory": "12300" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static auto _ = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n return nullptr;\n}();\nint nums[100001];\nint init= [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for(string s; getline(cin,s); cout << \"]\\n\") {\n cout << \"[\";\n int n{};\n for(int _i = s.size() - 2; _i >=0; --_i) {\n int v = s[_i--]&15;\n int p{1};\n while((s[_i]&15) < 10) {\n p*=10;\n v += p*(s[_i--]&15); \n }\n if (!n || nums[n - 1] <= v) {\n nums[n++] = v;\n }\n } \n cout << nums[n - 1];\n for (int i{n - 2}; i >= 0; --i) cout << ',' << nums[i];\n } \n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode *p=NULL,*q=head;\n\n while(q!=NULL){\n ListNode*temp = q->next;\n q->next=p;\n p=q;\n q=temp;\n }\n\n q=p->next;\n head=p;\n\n while(q!=NULL){\n if(p->val <= q->val){\n p=q;\n q=q->next;\n }else{\n p->next = q->next;\n q=q->next;\n }\n }\n p=head;\n q=NULL;\n\n while(p!=NULL){\n ListNode*temp = p->next;\n p->next=q;\n q=p;\n p=temp;\n }\n\n return q;\n \n }\n};", "memory": "10506" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static auto _ = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n return nullptr;\n}();\nint nums[100001];\nint init= [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for(string s; getline(cin,s); cout << \"]\\n\") {\n cout << \"[\";\n int n{};\n for(int _i = s.size() - 2; _i >=0; --_i) {\n int v = s[_i--]&15;\n int p{1};\n while((s[_i]&15) < 10) {\n p*=10;\n v += p*(s[_i--]&15); \n }\n if (!n || nums[n - 1] <= v) {\n nums[n++] = v;\n }\n } \n cout << nums[n - 1];\n for (int i{n - 2}; i >= 0; --i) cout << ',' << nums[i];\n } \n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(head==NULL||head->next==NULL){\n return head;//base case\n }\n int t=0;\n bool check=0;\n \n if(head->next!=NULL&&(head->next->val>head->val)){\n head=head->next;\n }\n return head;\n }\n};", "memory": "10506" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n ListNode* rev(ListNode* head){\n ListNode* prev,* curr,* next;\n prev=NULL,curr = head,next =head;\n while(next!=NULL){\n curr = next;\n next = curr->next;\n curr->next = prev;\n prev = curr;\n }\n return curr;\n }\n \npublic:\n ListNode* removeNodes(ListNode* head) {\n ios_base::sync_with_stdio(0);\n if(head == NULL || head->next == NULL)return head;\n head = rev(head);\n ListNode* prev,* head2;\n prev = head2 = head;\n int mx = 0;\n while(head!= NULL){\n if(head->val >= mx){\n if(prev!=head){\n prev->next = head;\n prev = head;}\n mx = head->val;\n }\n head=head->next;\n }\n prev->next = NULL;\n return rev(head2);\n }\n};", "memory": "12720" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n head = reverseList(head);\n\n int maxi = 0;\n ListNode * curr = head;\n ListNode * prev = nullptr;\n\n while(curr != nullptr){\n maxi = max(maxi,curr->val);\n\n if(curr->val < maxi){\n prev->next = curr->next;\n ListNode * toDelete = curr;\n curr = curr->next;\n toDelete->next = nullptr;\n // delete toDelete;\n }\n else{\n prev = curr;\n curr = curr->next;\n }\n }\n return reverseList(head);\n }\nprivate:\n ListNode * reverseList(ListNode * head){\n ListNode * curr = head;\n ListNode * prev = nullptr;\n ListNode * next = nullptr;\n\n while(curr != nullptr){\n next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n return prev;\n }\n};", "memory": "14934" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n \n ListNode* reverse(ListNode* head){\n ListNode* temp = head;\n ListNode* last = NULL;\n while(temp!=NULL){\n ListNode* front = temp->next;\n temp->next = last ;\n last = temp ;\n temp = front ;\n }\n return last;\n }\n \n ListNode* removeNodes(ListNode* head) {\n if(head ==NULL || head->next ==NULL){\n return head;\n }\n \n head = reverse(head) ;\n \n // ListNode* \n stack<ListNode*>st;\n st.push(head) ;\n \n ListNode* temp = head->next;\n ListNode* tempNode = head;\n while(temp!=NULL){\n while(!st.empty() && tempNode->val <= temp->val ){\n st.pop();\n if(!st.empty()) {\n tempNode = st.top();\n }\n \n }\n \n if(!st.empty()){\n tempNode->next = temp->next;\n // delete temp ;\n temp = tempNode->next;\n }\n else{\n st.push(temp) ;\n tempNode=temp;\n temp =temp->next;\n }\n }\n \n head = reverse(head) ;\n \n return head;\n }\n};", "memory": "14934" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n \n ListNode* prev = nullptr;\n ListNode* curr = head;\n while (curr != nullptr) {\n ListNode* nextNode = curr->next;\n curr->next = prev;\n prev = curr;\n curr = nextNode;\n }\n return prev;\n }\n ListNode* removeNodes(ListNode* head) {\n if(head==NULL){\n return NULL;\n }\n head=reverseList(head);\n\n ListNode* maxNode=head;\n ListNode* current=head;\n \n\n while(current!=NULL&&current->next!=NULL){\n if(current->next->val<maxNode->val){\n current->next=current->next->next;\n }\n else{\n current=current->next;\n maxNode=current;\n }\n }\n head=reverseList(head);\n return head;\n }\n};", "memory": "17148" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* prev = NULL;\n while(head!=NULL){\n ListNode* nex = head->next;\n head->next = prev;\n prev = head;\n head = nex;\n }\n head = prev;\n\n int maxlen=0;\n ListNode* temp =head;\n prev =NULL;\n while(head!=NULL){\n ListNode* nexx = head->next;\n if(head->val<maxlen){\n prev->next = nexx;\n }else{\n maxlen = max(maxlen,head->val);\n prev=head;\n }\n head = nexx;\n }\n\n head = temp;\n\n prev = NULL;\n while(head!=NULL){\n ListNode* nex = head->next;\n head->next = prev;\n prev = head;\n head = nex;\n }\n return prev;\n\n }\n};", "memory": "17148" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n head = reverseList(head);\n\n int maxi = 0;\n ListNode * curr = head;\n ListNode * prev = nullptr;\n\n while(curr != nullptr){\n maxi = max(maxi,curr->val);\n\n if(curr->val < maxi){\n prev->next = curr->next;\n ListNode * toDelete = curr;\n curr = curr->next;\n toDelete->next = nullptr;\n // delete toDelete;\n }\n else{\n prev = curr;\n curr = curr->next;\n }\n }\n return reverseList(head);\n }\nprivate:\n ListNode * reverseList(ListNode * head){\n ListNode * curr = head;\n ListNode * prev = nullptr;\n ListNode * next = nullptr;\n\n while(curr != nullptr){\n next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n return prev;\n }\n};", "memory": "19361" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode *prev=NULL,*curr=head,*nxt=NULL;\n while(curr)\n {\n nxt=curr->next;\n curr->next=prev;\n prev=curr;\n curr=nxt;\n }\n return head=prev;\n }\n ListNode* removeNodes(ListNode* head) {\n head = reverseList(head);\n int max = head->val;\n ListNode *p = head;\n while(p->next != NULL)\n {\n if(p->next->val < p->val)\n {\n p->next = p->next->next;\n }\n else\n {\n p = p->next;\n }\n }\n head = reverseList(head);\n return head;\n }\n};", "memory": "19361" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n\n ListNode* reverse(ListNode* head)\n {\n ListNode* p1 = head;\n ListNode* p2 = head->next;\n\n p1->next = nullptr;\n\n while(p2)\n {\n ListNode* temp = p2->next;\n p2->next = p1;\n p1 = p2;\n p2 = temp;\n }\n return p1;\n }\n ListNode* removeNodes(ListNode* head) {\n if(head == nullptr) return head;\n\n ListNode* rev_head = reverse(head);\n //return rev_head;\n ListNode* p1 = rev_head;\n ListNode* p2 = p1;\n\n int max_val = p1->val;\n\n while(p2)\n {\n if(p2->val >= max_val)\n {\n max_val = p2->val;\n p1= p2;\n }\n\n if(p2->val < max_val)\n {\n //delete p1\n p1->next = p2->next;\n } \n p2 = p2->next;\n }\n return reverse(rev_head);\n }\n};", "memory": "21575" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head){\n if(head==NULL || head->next==NULL) return head;\n ListNode * curr=head,*prev=NULL,*Next=head;\n while(curr){\n Next=curr->next;\n curr->next=prev;\n prev=curr;\n curr=Next;\n }\n return prev;\n }\n ListNode* removeNodes(ListNode* head) {\n if(head->next==NULL) return head;\n ListNode *temp=head;\n temp=reverseList(temp);\n ListNode *a=new ListNode(1);\n ListNode* b=a;\n int max=INT_MIN;\n while(temp){\n if(temp->val>=max){\n max=temp->val;\n b->next=temp;\n b=temp;\n }\n temp=temp->next;\n }\n b->next=NULL;\n b=reverseList(a->next);\n return b;\n }\n};", "memory": "21575" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n#include<cstdio>\nclass Solution {\npublic:\n ListNode* reverse(ListNode* p){\n ListNode* prev=NULL;\n ListNode* curr=p;\n ListNode* temp_next=NULL;\n\n while(curr){\n temp_next = curr->next;\n\n curr->next = prev;\n prev = curr;\n curr = temp_next;\n }\n\n return prev;\n }\n\n void printList(ListNode* p){\n printf(\"List: \\n\");\n while(p){\n printf(\"\\t%i\\n\", p->val);\n p=p->next;\n }\n }\n\n ListNode* removeNodes(ListNode* head) {\n ListNode* rev = reverse(head);\n ListNode* p =rev;\n printList(rev);\n\n while(p->next){\n if(p->val <= p->next->val){\n p=p->next;\n }\n else{\n p->next = p->next->next;\n }\n }\n\n return reverse(rev);\n }\n};", "memory": "23789" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* temp=head;\n int n=0;\n while(temp->next){\n n++;\n temp=temp->next;\n }\n n++;\n stack<int> st;;\n int nge[n];\n nge[n-1]=-1;\n st.push(temp->val);\n //reverse\n ListNode* currN=head,*prevN=NULL,*nextN;\n while(currN){\n nextN=currN->next;\n currN->next=prevN;\n prevN=currN;\n currN=nextN;\n }\n temp=prevN;\n temp=temp->next;\n int i=n-2;\n while(temp){\n while(st.size()>0 && st.top()<=temp->val) st.pop();\n if(st.size()==0) nge[i]=-1;\n else nge[i]=st.top();\n\n st.push(temp->val);\n temp=temp->next;\n i--;\n }\n //reverse\n currN=prevN,prevN=NULL;\n while(currN){\n nextN=currN->next;\n currN->next=prevN;\n prevN=currN;\n currN=nextN;\n }\n \n\n ListNode* c=new ListNode(10);\n ListNode* c2=c;\n c->next=head;\n temp=c;\n i=0;\n while(temp && temp->next){\n while(nge[i]!=-1){ \n temp=temp->next;\n i++;\n }\n \n temp=temp->next;\n c->next=temp;\n c=c->next;\n i++;\n \n }\n return c2->next;\n }\n};", "memory": "26003" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* temp=head;\n int n=0;\n while(temp->next){\n n++;\n temp=temp->next;\n }\n n++;\n stack<int> st;;\n int nge[n];\n nge[n-1]=-1;\n st.push(temp->val);\n //reverse\n ListNode* currN=head,*prevN=NULL,*nextN;\n while(currN){\n nextN=currN->next;\n currN->next=prevN;\n prevN=currN;\n currN=nextN;\n }\n temp=prevN;\n temp=temp->next;\n int i=n-2;\n while(temp){\n while(st.size()>0 && st.top()<=temp->val) st.pop();\n if(st.size()==0) nge[i]=-1;\n else nge[i]=st.top();\n\n st.push(temp->val);\n temp=temp->next;\n i--;\n }\n //reverse\n currN=prevN,prevN=NULL;\n while(currN){\n nextN=currN->next;\n currN->next=prevN;\n prevN=currN;\n currN=nextN;\n }\n \n\n ListNode* c=new ListNode(10);\n ListNode* c2=c;\n c->next=head;\n temp=c;\n i=0;\n while(temp->next){\n while(nge[i]!=-1){ \n temp=temp->next;\n i++;\n }\n \n temp=temp->next;\n c->next=temp;\n c=c->next;\n i++;\n \n }\n return c2->next;\n }\n};", "memory": "28216" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* current = head;\n ListNode* prev = NULL;\n ListNode* next = NULL;\n while(current!=NULL)\n {\n next = current->next;\n current->next = prev;\n prev = current;\n current = next;\n }\n head=prev;\n return head;\n }\n ListNode* removeNodes(ListNode* head) {\n stack<int>stk;\n ListNode* newhead=reverseList(head);\n stk.push(newhead->val);\n ListNode *temp=newhead->next;\n ListNode *prev=newhead;\n while(temp!=NULL)\n {\n if(stk.top()>temp->val)\n {\n prev->next=temp->next;\n temp=temp->next;\n }else{\n stk.push(temp->val);\n prev=temp;\n temp=temp->next;\n }\n }\n ListNode* ans=reverseList(newhead);\n return ans;\n }\n};", "memory": "30430" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* current = head;\n ListNode* prev = NULL;\n ListNode* next = NULL;\n while(current!=NULL)\n {\n next = current->next;\n current->next = prev;\n prev = current;\n current = next;\n }\n head=prev;\n return head;\n }\n ListNode* removeNodes(ListNode* head) {\n stack<int>stk;\n ListNode* newhead=reverseList(head);\n stk.push(newhead->val);\n ListNode *temp=newhead->next;\n ListNode *prev=newhead;\n while(temp!=NULL)\n {\n if(stk.top()>temp->val)\n {\n prev->next=temp->next;\n temp=temp->next;\n }else{\n stk.push(temp->val);\n prev=temp;\n temp=temp->next;\n }\n }\n ListNode* ans=reverseList(newhead);\n return ans;\n }\n};", "memory": "32644" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* current = head;\n ListNode* prev = NULL;\n ListNode* next = NULL;\n while(current!=NULL)\n {\n next = current->next;\n current->next = prev;\n prev = current;\n current = next;\n }\n head=prev;\n return head;\n }\n ListNode* removeNodes(ListNode* head) {\n stack<int>stk;\n ListNode* newhead=reverseList(head);\n stk.push(newhead->val);\n ListNode *temp=newhead->next;\n ListNode *prev=newhead;\n while(temp!=NULL)\n {\n if(stk.top()>temp->val)\n {\n prev->next=temp->next;\n temp=temp->next;\n }else{\n stk.push(temp->val);\n prev=temp;\n temp=temp->next;\n }\n }\n ListNode* ans=reverseList(newhead);\n return ans;\n }\n};", "memory": "32644" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n\nint removeNonMax(ListNode* prev_it){\n int res = 0;\n if(prev_it->next){\n res = removeNonMax(prev_it->next);\n if(res>prev_it->next->val){\n ListNode* to_delete = prev_it->next;\n prev_it->next = to_delete->next;\n //delete to_delete;\n }else{\n res = prev_it->next->val;\n }\n }\n return res;\n}\n\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode dummy_head(0,head);\n removeNonMax(&dummy_head);\n return dummy_head.next;\n }\n};\n\n", "memory": "34858" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n deque<int> d;\n ListNode* temp=head;\n // count=0;\n while(temp!=NULL)\n {\n int ele=temp->val;\n if(d.empty()) d.push_back(ele);\n else{\n while(!d.empty() && d.back()<ele){\n d.pop_back();\n }\n d.push_back(ele);\n }\n temp=temp->next;\n }\n temp=head;\n ListNode* main=new ListNode(0,nullptr);\n ListNode* dummy=main;\n while(!d.empty()){\n if(temp->val==d.front()){\n dummy->next=temp;\n d.pop_front();\n dummy=dummy->next;\n }\n temp=temp->next;\n }\n return main->next;\n }\n};", "memory": "37071" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n\nint removeNonMax(ListNode* prev_it){\n int res = 0;\n if(prev_it->next){\n res = removeNonMax(prev_it->next);\n if(res>prev_it->next->val){\n ListNode* to_delete = prev_it->next;\n prev_it->next = to_delete->next;\n //delete to_delete;\n }else{\n res = prev_it->next->val;\n }\n }\n return res;\n}\n\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode dummy_head(0,head);\n removeNonMax(&dummy_head);\n return dummy_head.next;\n }\n};\n\n", "memory": "39285" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode *prev , *slow , *fast;\n vector<int>nums;\n prev = NULL;\n slow = head;\n while(slow)\n {\n fast = slow->next;\n slow->next = prev;\n prev = slow;\n slow = fast;\n }\n head = prev;\n prev = head;\n slow = head->next;\n nums.push_back(head->val);\n while(slow)\n {\n if(slow->val >= nums[nums.size()-1])\n {\n nums.push_back(slow->val);\n prev = slow;\n slow = slow->next;\n }\n else\n {\n prev->next = slow->next;\n slow = prev->next;\n }\n }\n prev = NULL;\n slow = head;\n while(slow)\n {\n fast = slow->next;\n slow->next = prev;\n prev = slow;\n slow = fast;\n }\n head = prev;\n return head;\n }\n};", "memory": "41499" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* ptr1 = head;\n ListNode* ptr2 = head -> next;\n /*\n while(ptr1)\n {\n if(ptr2 -> val > ptr1 -> val)\n {\n *ptr1 = *ptr1 -> next;\n highest = ptr2 -> val;\n }\n else\n {\n ptr2 = ptr2 -> next;\n }\n if(!ptr2)\n {\n ptr1 = ptr1 -> next;\n ptr2 = ptr1;\n }\n }\n return head;\n */\n while(ptr2)\n {\n if(ptr2 -> val > head -> val)\n {\n head = ptr2;\n ptr1 = head;\n ptr2 = ptr2 -> next;\n }\n else if(ptr2 -> val > ptr1 -> val)\n {\n *ptr1 = *ptr2;\n delete ptr2;\n ptr2 = ptr1;\n ptr1 = head -> next;\n }\n else if(ptr1 -> next == ptr2 -> next) \n {\n ptr2 = ptr2 -> next;\n }\n else\n ptr1 = ptr1 -> next;\n }\n return head;\n }\n};", "memory": "43713" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* ptr1 = head;\n ListNode* ptr2 = head -> next;\n /*\n while(ptr1)\n {\n if(ptr2 -> val > ptr1 -> val)\n {\n *ptr1 = *ptr1 -> next;\n highest = ptr2 -> val;\n }\n else\n {\n ptr2 = ptr2 -> next;\n }\n if(!ptr2)\n {\n ptr1 = ptr1 -> next;\n ptr2 = ptr1;\n }\n }\n return head;\n */\n while(ptr2)\n {\n if(ptr2 -> val > head -> val)\n {\n head = ptr2;\n ptr1 = head;\n ptr2 = ptr2 -> next;\n }\n else if(ptr2 -> val > ptr1 -> val)\n {\n *ptr1 = *ptr2;\n delete ptr2;\n ptr2 = ptr1;\n ptr1 = head -> next;\n }\n else if(ptr1 -> next == ptr2 -> next) \n {\n ptr2 = ptr2 -> next;\n }\n else\n ptr1 = ptr1 -> next;\n }\n return head;\n }\n};", "memory": "43713" }
2,573
<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>
0
{ "code": "class Solution {\npublic:\n ListNode* reverse(ListNode* head){\n ListNode* prev = NULL;\n ListNode* curr = head;\n while(curr){\n ListNode* next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n return prev; \n }\n \n ListNode* removeNodes(ListNode* head) {\n head = reverse(head);\n\n stack<ListNode*> st;\n while(head){\n if(st.empty() || st.top()->val <= head->val ){\n st.push(head);\n }\n head = head->next;\n }\n\n ListNode* ans = new ListNode(0); \n ListNode* temp = ans; \n while(!st.empty()){\n temp->next = st.top();\n temp = temp->next;\n st.pop();\n }\n temp->next = NULL; \n return ans->next;\n }\n};\n", "memory": "45926" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*> s;\n // reverse the list\n ListNode* prev = nullptr;\n ListNode* cur = head;\n ListNode* next = nullptr;\n while(cur){\n next = cur->next;\n cur->next = prev;\n prev = cur;\n cur = next;\n }\n head = prev;\n int maxE = head->val;\n ListNode* tmp = head;\n while(tmp){\n if(tmp->val >= maxE){\n s.push(tmp);\n maxE = tmp->val;\n }\n tmp = tmp->next;\n }\n \n ListNode* newH = s.top();\n ListNode* dum = newH;\n s.pop();\n while(!s.empty()){\n newH->next = s.top();\n s.pop();\n newH = newH->next;\n }\n newH->next = nullptr;\n return dum;\n\n }\n};", "memory": "48140" }
2,573
<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>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode* head)\n {\n ListNode* prev=NULL,*temp=head;\n while(temp)\n {\n ListNode* front=temp->next;\n temp->next=prev;\n prev=temp;\n temp=front;\n }\n return prev;\n }\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*>st;\n ListNode* newhead=reverse(head);\n ListNode* temp=newhead,*prev=NULL;\n st.push(temp);\n while(temp!=NULL)\n {\n if(temp->val<st.top()->val)\n {\n if(prev!=NULL)\n prev->next=temp->next;\n }\n else\n {\n st.push(temp);\n prev=temp;\n }\n temp=temp->next;\n }\n return reverse(newhead);\n }\n};", "memory": "50354" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(head==NULL){\n return head;\n }\n ListNode* node=head;\n ListNode* nxtGreater=removeNodes(node->next);\n node->next = nxtGreater;\n if (nxtGreater == nullptr || node->val >= nxtGreater->val) {\n return node;\n }\n return nxtGreater;\n }\n};", "memory": "63636" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n int solve(ListNode* &head)\n {\n if(head == NULL)\n {\n return 0;\n }\n int ans = 0;\n ans = max(head->val,solve(head->next));\n if(head->val<ans)\n {\n head->val = -1;\n }\n return ans;\n }\n ListNode* removeNodes(ListNode* head) {\n solve(head);\n \n while(head!=NULL&&head->val == -1)\n {\n head = head->next;\n }\n\n ListNode* prev = NULL;\n ListNode* curr = head;\n while(curr!=NULL)\n {\n cout<<curr->val<<\" \";\n if(curr->val == -1)\n {\n prev->next = curr->next;\n curr = curr->next;\n continue;\n }\n prev = curr;\n curr = curr->next;\n }\n return head;\n }\n};", "memory": "63636" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\nint maxRight = INT_MIN;\n ListNode* removeNodes(ListNode* head) {\n stack<int> s,t;\n ListNode* temp = head;\n while(head){\n while(!s.empty() && head->val > s.top()) s.pop();\n s.push(head->val);\n head = head->next;\n }\n while(!s.empty()){\n t.push(s.top());\n s.pop();\n }\n head = temp;\n while(1){\n head->val = t.top();\n t.pop();\n if(t.empty())\n break;\n head = head->next;\n }\n head->next = NULL;\n return temp;\n }\n};", "memory": "65850" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode*p=head;\n stack<int> m;\n while(p!=NULL)\n {\n if(m.empty())\n {\n m.push(p->val);\n p=p->next;\n }\n else\n {\n if(p->val<=m.top())\n {\n m.push(p->val);\n p=p->next;\n\n }\n else\n {\n while(!m.empty()&&p->val>m.top())\n {\n m.pop();\n }\n m.push(p->val);\n p=p->next;\n }\n }\n }\n p=head;\n stack<int> n;\n while(!m.empty())\n {\n n.push(m.top());\n m.pop();\n }\n while(!n.empty())\n {\n p->val=n.top();\n n.pop();\n if(n.empty())\n {\n p->next=NULL;\n }\n else\n {\n p=p->next;\n }\n \n }\n \n return head;\n\n \n }\n};", "memory": "68064" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n vector<int> mono;\n ListNode* cur = head;\n while(cur){\n while(!mono.empty() && mono.back() < cur->val){\n mono.pop_back();\n }\n mono.push_back(cur->val);\n cur = cur->next;\n }\n ListNode* dummy = new ListNode(0, nullptr);\n ListNode* cur_dummy = dummy;\n cur = head;\n int i = 0;\n while(cur){\n if(cur->val == mono[i]){\n cur_dummy->next = cur;\n cur_dummy = cur_dummy->next;\n i++;\n }\n cur = cur->next;\n }\n return dummy->next;\n\n }\n};", "memory": "70278" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(!head) return head;\n vector<int> st;\n for(auto* node = head; node; node = node->next) {\n while(st.size() and node->val > st.back()){\n st.pop_back();\n }\n st.push_back(node->val);\n }\n \n auto* node = head;\n for(auto i = 0; i < st.size()-1; i++){\n node->val = st[i];\n node = node->next;\n }\n node->val = st.back();\n node->next = nullptr;\n\n return head;\n }\n};", "memory": "72491" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n vector<int> mono;\n ListNode* cur = head;\n while(cur){\n while(!mono.empty() && mono.back() < cur->val){\n mono.pop_back();\n }\n mono.push_back(cur->val);\n cur = cur->next;\n }\n ListNode* dummy = new ListNode(0, nullptr);\n ListNode* cur_dummy = dummy;\n cur = head;\n int i = 0;\n while(cur){\n if(cur->val == mono[i]){\n cur_dummy->next = cur;\n cur_dummy = cur_dummy->next;\n i++;\n }\n cur = cur->next;\n }\n ListNode* temp = dummy->next;\n delete dummy;\n return temp;\n\n }\n};", "memory": "74705" }
2,573
<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>
1
{ "code": "class Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* cur = head;\n vector<int> st;\n\n \n while (cur) {\n \n while (!st.empty() && cur->val > st.back()) {\n st.pop_back(); \n }\n st.push_back(cur->val); \n cur = cur->next;\n }\n reverse(st.begin(), st.end());\n ListNode* dummy = new ListNode(0);\n ListNode* prev = dummy;\n cur = head;\n\n while (cur && !st.empty()) {\n if (cur->val == st.back()) {\n prev->next = cur;\n prev = cur;\n st.pop_back(); \n }\n cur = cur->next;\n }\n\n prev->next = nullptr; \n return dummy->next;\n }\n};\n", "memory": "76919" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* deleteNode(ListNode* head, ListNode* node, ListNode* prev) {\n if (head == nullptr || node == nullptr) {\n return head;\n }\n if (head == node) {\n ListNode* newHead = head->next;\n delete head;\n return newHead;\n }\n if (prev->next == node) {\n prev->next = node->next;\n delete node;\n }\n\n return head;\n }\n ListNode* reverse(ListNode* head) {\n if (head == nullptr || head->next == nullptr) {\n return head;\n }\n ListNode* p = head;\n ListNode* q = nullptr;\n ListNode* r = nullptr;\n\n while (p != nullptr) {\n q = p;\n p = p->next;\n q->next = r;\n r = q;\n }\n return q;\n }\n ListNode* removeNodes(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n if (head == nullptr || head->next == nullptr) {\n return head;\n }\n head = reverse(head);\n\n ListNode* temp = head;\n int maxi = temp->val;\n ListNode* prev = temp;\n temp = temp->next;\n\n while (temp != nullptr) {\n if (temp->val < maxi) {\n ListNode* t = temp->next;\n head = deleteNode(head, temp, prev);\n temp = t;\n } else {\n maxi = temp->val;\n prev = temp;\n temp = temp->next;\n }\n }\n head = reverse(head);\n return head;\n }\n};", "memory": "79133" }
2,573
<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>
1
{ "code": "class Solution {\npublic:\n // Helper function to reverse the linked list\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* curr = head;\n while (curr != nullptr) {\n ListNode* next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n return prev;\n }\n\n ListNode* removeNodes(ListNode* head) {\n if (!head || !head->next) return head;\n\n // Step 1: Reverse the linked list\n head = reverseList(head);\n\n // Step 2: Traverse the reversed list and remove nodes with smaller values\n ListNode* current = head;\n int maxVal = current->val;\n while (current != nullptr && current->next != nullptr) {\n if (current->next->val < maxVal) {\n // Remove the node since its value is smaller than the max seen so far\n ListNode* temp = current->next;\n current->next = temp->next;\n delete temp;\n } else {\n // Update max value and move to the next node\n maxVal = current->next->val;\n current = current->next;\n }\n }\n\n // Step 3: Reverse the list back to original order\n return reverseList(head);\n }\n};\n", "memory": "81346" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n \n ListNode* reverse(ListNode* head){\n ListNode* temp = head;\n ListNode* last = NULL;\n while(temp!=NULL){\n ListNode* front = temp->next;\n temp->next = last ;\n last = temp ;\n temp = front ;\n }\n return last;\n }\n \n ListNode* removeNodes(ListNode* head) {\n if(head ==NULL || head->next ==NULL){\n return head;\n }\n \n head = reverse(head) ;\n \n // ListNode* \n stack<ListNode*>st;\n st.push(head) ;\n \n ListNode* temp = head->next;\n ListNode* tempNode = head;\n while(temp!=NULL){\n while(!st.empty() && tempNode->val <= temp->val ){\n st.pop();\n if(!st.empty()) {\n tempNode = st.top();\n }\n \n }\n \n if(!st.empty()){\n tempNode->next = temp->next;\n delete temp ;\n temp = tempNode->next;\n }\n else{\n st.push(temp) ;\n tempNode=temp;\n temp =temp->next;\n }\n }\n \n head = reverse(head) ;\n \n return head;\n }\n};", "memory": "83560" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n \n ListNode* reverse(ListNode* head){\n ListNode* temp = head;\n ListNode* last = NULL;\n while(temp!=NULL){\n ListNode* front = temp->next;\n temp->next = last ;\n last = temp ;\n temp = front ;\n }\n return last;\n }\n \n ListNode* removeNodes(ListNode* head) {\n if(head ==NULL || head->next ==NULL){\n return head;\n }\n \n \n// ******************************** METHOD 1 ********* \n// head = reverse(head) ;\n \n// // ListNode* \n// stack<ListNode*>st;\n// st.push(head) ;\n \n// ListNode* temp = head->next;\n// ListNode* tempNode = head;\n// while(temp!=NULL){\n// while(!st.empty() && tempNode->val <= temp->val ){\n// st.pop();\n// if(!st.empty()) {\n// tempNode = st.top();\n// }\n \n// }\n \n// if(!st.empty()){\n// tempNode->next = temp->next;\n// // delete temp ;\n// temp = tempNode->next;\n// }\n// else{\n// st.push(temp) ;\n// tempNode=temp;\n// temp =temp->next;\n// }\n// }\n \n// head = reverse(head) ;\n \n// return head;\n \n// ******************************************** METHOD 2 **************************** \n head = reverse(head) ;\n ListNode* temp = head->next ;\n ListNode* prev = head;\n int maxVal = head->val ;\n \n while(temp!=NULL){\n if(temp->val < maxVal){\n ListNode* tempNext = temp->next;\n delete temp ;\n prev->next = tempNext ;\n temp = tempNext;\n }\n else{\n prev= temp ;\n temp = temp->next;\n maxVal = prev->val;\n }\n }\n \n head = reverse(head) ;\n \n return head;\n }\n};", "memory": "85774" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\nprivate:\n ListNode* reverse(ListNode* head){\n ListNode* prev = nullptr;\n ListNode* temp = head;\n while(temp){\n ListNode* next = temp->next;\n temp->next = prev;\n prev = temp;\n temp = next;\n } \n return prev;\n\n }\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(!head && !head -> next) return head;\n head = reverse(head);\n ListNode* max = head;\n ListNode* temp = head;\n \n while(temp->next){\n if(temp-> val > temp -> next -> val) {\n ListNode* removalNode = temp -> next;\n temp -> next = removalNode -> next;\n delete removalNode;\n }\n else {\n max = temp;\n temp = temp -> next;\n }\n\n }\n\n return reverse(head);\n\n }\n};", "memory": "85774" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode*reverse(ListNode*head){\n ListNode*cur=head,*prev=NULL;\n while(cur){\n ListNode*temp=cur;\n cur=cur->next;\n temp->next=prev;\n prev=temp;\n }\n return prev;\n }\n\n\n ListNode* removeNodes(ListNode* head) {\n ListNode*NewHead=reverse(head);\n ListNode*dummy=new ListNode(-1);\n ListNode*p2=NewHead,*p1=dummy;\n while(p2){\n if(p2->val<p1->val){\n ListNode*temp=p2;\n p2=p2->next;\n temp->next=NULL;\n p1->next=p2;\n delete(temp);\n }\n else{\n p1->next=p2;\n p1=p1->next;\n p2=p2->next;\n }\n }\n // return dummy->next;\n return reverse(dummy->next);\n }\n};", "memory": "87988" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\nprivate:\n ListNode* reverse(ListNode* head){\n ListNode* prev = nullptr;\n ListNode* temp = head;\n while(temp){\n ListNode* next = temp->next;\n temp->next = prev;\n prev = temp;\n temp = next;\n } \n return prev;\n\n }\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(!head && !head -> next) return head;\n head = reverse(head);\n ListNode* max = head;\n ListNode* temp = head;\n \n while(temp->next){\n if(temp-> val > temp -> next -> val) {\n ListNode* removalNode = temp -> next;\n temp -> next = removalNode -> next;\n delete removalNode;\n }\n else {\n max = temp;\n temp = temp -> next;\n }\n\n }\n\n return reverse(head);\n\n }\n};", "memory": "87988" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode* head) {\n if (head == nullptr or head->next == nullptr) return head;\n \n ListNode* first = head;\n ListNode* second = first->next;\n \n while (second) {\n first->next = second->next;\n second->next = head;\n head = second;\n second = first->next;\n }\n \n return head;\n }\n ListNode* removeNodes(ListNode* head) {\n head = reverse(head);\n ListNode* current = head;\n ListNode* last = head;\n while (current) {\n if (current->val >= last->val and current != head) {\n last->next = current;\n last = current;\n current = current->next;\n }\n else if (current->val < last->val) {\n ListNode* temp = current;\n current = current->next;\n delete temp;\n }\n else current = current->next;\n }\n last->next = nullptr;\n head = reverse(head);\n\n return head;\n }\n};", "memory": "90201" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) \n {\n int len = 0;\n ListNode* curr = head;\n stack<int> values;\n while(curr != NULL)\n {\n len++;\n values.push(curr->val);\n curr = curr->next;\n }\n int greatest[len];\n int max = 0, i = len-1;\n while(!values.empty())\n {\n if(values.top() > max)\n {\n max = values.top();\n }\n values.pop();\n greatest[i--] = max;\n }\n ListNode* dummy = new ListNode(0, head), *newList = dummy;\n curr = head;\n i=1;\n while(curr->next != NULL)\n {\n if(greatest[i++] <= curr->val)\n {\n newList->next = curr;\n newList = newList->next;\n }\n curr = curr->next;\n }\n newList->next = curr;\n return dummy->next;\n \n }\n};", "memory": "92415" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head){\n if(head==NULL || head->next==NULL) return head;\n ListNode* newHead = reverseList(head->next);\n head->next->next = head;\n head->next = NULL;\n return newHead;\n } \n ListNode* removeNodes(ListNode* head) {\n head = reverseList(head);\n stack<int> st;\n st.push(head->val);\n ListNode* temp = head;\n while(temp->next){\n if(temp->next->val<st.top()) temp->next = temp->next->next;\n else if(temp->next->val>st.top()){\n st.push(temp->next->val); \n temp = temp->next;\n }\n else temp = temp->next;\n }\n return reverseList(head);\n }\n};", "memory": "94629" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head){\n if(head==NULL || head->next==NULL) return head;\n ListNode* newHead = reverseList(head->next);\n head->next->next = head;\n head->next = NULL;\n return newHead;\n } \n ListNode* removeNodes(ListNode* head) {\n head = reverseList(head);\n stack<int> st;\n st.push(head->val);\n ListNode* temp = head;\n while(temp->next){\n if(temp->next->val<st.top()) temp->next = temp->next->next;\n else if(temp->next->val>st.top()){\n st.push(temp->next->val); \n temp = temp->next;\n }\n else temp = temp->next;\n }\n return reverseList(head);\n }\n};", "memory": "94629" }
2,573
<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>
1
{ "code": "auto speedUP = [](){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\n\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*> stk;\n\n ListNode* curr = head;\n while(curr != nullptr){\n \n while(!stk.empty() && stk.top()->val < curr->val){\n stk.pop();\n }\n\n stk.push(curr);\n curr = curr->next;\n }\n\n ListNode* temp = nullptr;\n while(!stk.empty()){\n curr = stk.top();\n curr->next = temp;\n temp = curr;\n stk.pop();\n }\n\n return curr;;\n \n }\n};", "memory": "96843" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cout.tie(NULL);\n cin.tie(NULL);\n ListNode *ans = head;\n stack<ListNode*> s;\n while(head!=NULL)\n {\n while(!s.empty() && s.top()->val<head->val)\n {\n s.pop();\n }\n if(s.empty())\n ans = head;\n else\n s.top()->next = head;\n s.push(head);\n head = head->next;\n }\n return ans;\n }\n};", "memory": "99056" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* cur = head;\n stack<ListNode*> stack;\n \n while (cur != nullptr) {\n while (!stack.empty() && stack.top()->val < cur->val) {\n stack.pop();\n }\n stack.push(cur);\n cur = cur->next;\n }\n \n ListNode* nxt = nullptr;\n while (!stack.empty()) {\n cur = stack.top();\n stack.pop();\n cur->next = nxt;\n nxt = cur;\n }\n \n return cur;\n }\n};", "memory": "101270" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* cur = head;\n stack<ListNode*> stack;\n \n while (cur != nullptr) {\n while (!stack.empty() && stack.top()->val < cur->val) {\n stack.pop();\n }\n stack.push(cur);\n cur = cur->next;\n }\n \n ListNode* nxt = nullptr;\n while (!stack.empty()) {\n cur = stack.top();\n stack.pop();\n cur->next = nxt;\n nxt = cur;\n }\n \n return cur; \n }\n};", "memory": "103484" }
2,573
<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>
1
{ "code": "class Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*> st;\n // traverse, push stack, pop until st is empty or \n ListNode* curr = head;\n while (curr != nullptr)\n {\n while (!st.empty() && st.top()->val < curr->val)\n st.pop();\n st.push(curr);\n curr = curr->next;\n }\n ListNode* new_head, * nex_node = nullptr;\n while (!st.empty())\n {\n ListNode* curr_node = st.top();\n st.pop();\n curr_node->next = nex_node;\n nex_node = curr_node;\n if (st.empty())\n new_head = curr_node;\n }\n return new_head;\n }\n};", "memory": "103484" }
2,573
<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>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* curr=head;\n stack<ListNode*>st;\n while(curr!=nullptr){\n while(!st.empty()&& st.top()->val < curr->val){\n st.pop();\n }\n st.push(curr);\n curr=curr->next;\n } \n\n ListNode* nex=nullptr;\n while(!st.empty()){\n curr=st.top();\n st.pop();\n curr->next=nex;\n nex=curr;\n }\n return curr;\n \n }\n};", "memory": "105698" }
2,573
<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>
1
{ "code": "class Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*> st;\n // traverse, push stack, pop until st is empty or \n ListNode* curr = head;\n while (curr != nullptr)\n {\n while (!st.empty() && st.top()->val < curr->val)\n st.pop();\n st.push(curr);\n curr = curr->next;\n }\n ListNode* new_head, * nex_node = nullptr;\n while (!st.empty())\n {\n ListNode* curr_node = st.top();\n st.pop();\n curr_node->next = nex_node;\n nex_node = curr_node;\n if (st.empty())\n return curr_node;\n }\n return head;\n }\n};", "memory": "105698" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* curr=head;\n stack<ListNode*>st;\n while(curr!=nullptr){\n while(!st.empty()&& st.top()->val < curr->val){\n st.pop();\n }\n st.push(curr);\n curr=curr->next;\n } \n\n ListNode* nex=nullptr;\n while(!st.empty()){\n curr=st.top();\n st.pop();\n curr->next=nex;\n nex=curr;\n }\n return curr;\n }\n};", "memory": "107911" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* curr=head;\n stack<ListNode*>st;\n while(curr!=nullptr){\n while(!st.empty()&& st.top()->val < curr->val){\n st.pop();\n }\n st.push(curr);\n curr=curr->next;\n } \n\n ListNode* nex=nullptr;\n while(!st.empty()){\n curr=st.top();\n st.pop();\n curr->next=nex;\n nex=curr;\n }\n return curr;\n \n }\n};", "memory": "107911" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\nint maxRight = INT_MIN;\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*> s;\n ListNode* dummy = new ListNode(INT_MAX);\n s.push(dummy);\n while(head){\n while(s.top()->val < head->val) {\n s.pop();\n }\n s.top()->next = head;\n s.push(head);\n head = head->next;\n }\n return dummy->next;\n }\n};", "memory": "110125" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n vector<ListNode*> stk;\n stk.reserve(100000);\n while(head){\n while(!stk.empty() && stk.back()->val < head->val){\n stk.pop_back();\n }\n stk.emplace_back(head);\n head = head->next;\n }\n auto b = stk.begin(), e = stk.end();\n while(++b < e){\n (*(b-1))->next = *b;\n }\n return stk.front();\n }\n};", "memory": "112339" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n#include <stack>\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* temp = head;\n stack<int> stk1;\n\n while(temp != nullptr){\n stk1.push(temp -> val);\n temp = temp -> next;\n }\n\n stack<int> stk2;\n int curr = stk1.top();\n while(!stk1.empty()){\n if(stk1.top() >= curr){\n curr = stk1.top();\n stk2.push(curr);\n }\n stk1.pop();\n }\n\n ListNode* prev = nullptr;\n temp = head;\n while(!stk2.empty()){\n temp -> val = stk2.top();\n stk2.pop();\n prev = temp;\n temp = temp -> next;\n }\n prev -> next = nullptr;\n return head;\n }\n};", "memory": "114553" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n \n stack<int> s,s1;\n\n ListNode* temp = head;\n\n while(temp != NULL){\n s.push(temp->val);\n temp = temp->next;\n }\n\n int no = s.top();\n s.pop();\n\n s1.push(no);\n\n\n while(!s.empty()){\n\n int node = s.top();\n s.pop();\n\n if(node >= no){\n s1.push(node);\n no = node;\n }\n }\n\n temp = head;\n ListNode* prev = nullptr;\n \n while(!s1.empty()){\n\n int node = s1.top();\n s1.pop();\n\n temp->val = node;\n prev = temp;\n temp = temp->next;\n }\n\n prev->next = nullptr;\n return head;\n }\n};", "memory": "116766" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n \n stack<int> s,s1;\n\n ListNode* temp = head;\n\n while(temp != NULL){\n s.push(temp->val);\n temp = temp->next;\n }\n\n int no = s.top();\n s.pop();\n\n s1.push(no);\n\n\n while(!s.empty()){\n\n int node = s.top();\n s.pop();\n\n if(node >= no){\n s1.push(node);\n no = node;\n }\n }\n\n temp = head;\n ListNode* prev = nullptr;\n \n while(!s1.empty()){\n\n int node = s1.top();\n s1.pop();\n\n temp->val = node;\n prev = temp;\n temp = temp->next;\n }\n\n prev->next = nullptr;\n return head;\n }\n};", "memory": "118980" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode* head) {\n if (head == nullptr || head->next == nullptr) {\n return head;\n }\n\n // Recursively reverse the rest of the list\n ListNode* newHead = reverse(head->next);\n\n // Reverse the current node\n head->next->next = head; // Point next node's next to current node\n head->next = nullptr; // Set current node's next to null\n\n return newHead;\n }\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*> s;\n ListNode* nl = reverse(head);\n ListNode* temp = nl;\n s.push(temp);\n temp = temp->next;\n while (temp) {\n if(temp->val >= s.top()->val){\n s.push(temp);\n\n }\n temp= temp->next;\n\n }\n ListNode* ans = s.top();\n ListNode* anst = ans;\n s.pop();\n while(s.size()>0){\n anst->next = s.top();\n s.pop();\n \n anst = anst->next;\n }\n anst->next = nullptr;\n return ans;\n\n }\n};", "memory": "121194" }
2,573
<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>
2
{ "code": "class Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n \n\n // tc: O(n)\n // sc: (2n)\n\n ListNode* temp = head;\n ListNode* it = head;\n\n stack<int> stk;\n \n\n while( it != NULL )\n {\n stk.push(it->val);\n it = it->next;\n }\n\n int mx = 0;\n vector<int> v;\n\n while( !stk.empty() )\n {\n int top = stk.top();\n if( mx <= top )\n {\n v.push_back(top);\n mx = top;\n }\n\n stk.pop();\n }\n\n reverse(v.begin(),v.end());\n\n for(auto i : v)\n {\n temp -> val = i;\n it = temp;\n temp = temp->next;\n }\n if( it )\n it->next = NULL;\n\n return head;\n\n }\n};", "memory": "123408" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode* &head)\n {\n if(head == NULL || head->next == NULL)\n {\n return head;\n }\n ListNode* rev = reverse(head->next);\n head->next->next = head;\n head->next = NULL;\n return rev;\n }\n ListNode* removeNodes(ListNode* head) \n {\n ListNode* prev = reverse(head);\n int maxi = prev -> val;\n ListNode* curr = prev;\n ListNode* res = prev;\n while(curr != NULL)\n {\n if(curr->val < maxi)\n {\n prev->next = curr->next;\n ListNode* temp = curr;\n curr = curr->next;\n delete temp;\n }\n else\n {\n prev = curr;\n maxi = curr->val;\n curr = curr->next;\n }\n }\n ListNode* ans = reverse(res);\n\n return ans;\n }\n};", "memory": "125621" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<int> val;\n stack<ListNode*> node;\n node.push(head);\n val.push(head->val);\n ListNode* next = head->next;\n while(next){\n while(!val.empty() && val.top() < next->val){\n val.pop();\n node.pop();\n }\n node.push(next);\n val.push(next->val);\n next = next->next;\n }\n next = nullptr;\n while(!node.empty()){\n node.top()->next = next;\n next = node.top();\n node.pop();\n }\n return next;\n }\n};", "memory": "127835" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n#pragma GCC optimize(\"O3\", \"unroll-loops\")\n\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) { \n ListNode dummy=ListNode(100001);\n vector<ListNode*> stack={&dummy};\n for (ListNode* curr=head; curr; curr = curr->next) {\n while (curr->val > stack.back()->val) {\n stack.back()=NULL;\n stack.pop_back();\n }\n stack.back()->next = curr;\n stack.push_back(curr); \n }\n return dummy.next;\n }\n};\n\n\n\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "130049" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(head==NULL)\n return head;\n stack<int> s;\n ListNode* rh=head;\n ListNode* rrh=head;\n while(head!=NULL)\n {\n s.push(head->val);\n head=head->next;\n }\n stack<int> del;\n int temp=0;\n while(!s.empty())\n {\n if(s.top()>=temp)\n {\n temp=s.top();\n s.pop();\n }\n else\n {\n del.push(s.top());\n s.pop();\n }\n }\n while(!del.empty() && rh->val==del.top())\n {\n rh=rh->next;\n del.pop();\n }\n head=rh;\n while(!del.empty() && rh->next!=NULL)\n {\n if(rh->next->val==del.top())\n { \n rh->next=rh->next->next;\n del.pop();\n }\n else\n rh=rh->next;\n }\n return head; \n }\n};", "memory": "132263" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n \n ListNode* prev = nullptr;\n ListNode* curr = head;\n\n while (curr != nullptr) {\n ListNode* nextNode = curr->next;\n curr->next = prev;\n prev = curr;\n curr = nextNode;\n }\n \n ListNode* reversedHead = prev;\n if (!reversedHead) return nullptr; \n\n \n ListNode* newHead = nullptr;\n int maxval = reversedHead->val; \n\n curr = reversedHead;\n\n while (curr != nullptr) {\n if (curr->val >= maxval) {\n maxval = curr->val; // Update maxval\n\n \n ListNode* newNode = new ListNode(curr->val);\n newNode->next = newHead;\n newHead = newNode;\n }\n curr = curr->next;\n }\n\n return newHead; \n }\n};", "memory": "134476" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void ReverseLinkedList(ListNode* &head){\n ListNode* prevPtr = NULL;\n ListNode* currPtr = head;\n ListNode* nextPtr = NULL;\n while(currPtr!=NULL){\n nextPtr = currPtr->next;\n currPtr->next = prevPtr;\n prevPtr = currPtr;\n currPtr = nextPtr;\n }\n head = prevPtr;\n }\n ListNode* removeNodes(ListNode* head) {\n ReverseLinkedList(head);\n int currMax = head->val;\n ListNode* tempPtr = head->next;\n ListNode* ll = new ListNode(currMax);\n while(tempPtr!=NULL){\n int num = tempPtr->val;\n if(num >= currMax){\n currMax = num;\n ListNode* n = new ListNode(num);\n if(ll==NULL){\n ll = n;\n }else{\n n->next = ll;\n ll = n;\n }\n }\n tempPtr = tempPtr->next;\n }\n return ll;\n }\n};", "memory": "136690" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* curr = head;\n while (curr != nullptr) {\n ListNode* next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n return prev;\n }\n ListNode* removeNodes(ListNode* head) {\n\n head = reverseList(head);\n \n ListNode* newHead = nullptr;\n ListNode* prev = nullptr;\n int maxVal = INT_MIN;\n\n while (head != nullptr) {\n if (head->val >= maxVal) {\n ListNode* temp = new ListNode(head->val);\n if (newHead == nullptr) {\n newHead = temp;\n } else {\n prev->next = temp;\n }\n prev = temp;\n maxVal = head->val;\n }\n head = head->next;\n }\n \n \n return reverseList(newHead);\n }\n};", "memory": "136690" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head)\n {\n vector<ListNode*> buf;\n\n ListNode* curr = head;\n while (curr)\n {\n while (buf.size() > 0 && buf.back()->val < curr->val) {\n buf.pop_back();\n }\n buf.push_back(curr);\n\n curr = curr->next;\n }\n\n ListNode* ret = new ListNode();\n curr = ret;\n\n for (int i = 0; i < buf.size(); i++) {\n curr->next = buf[i];\n curr = curr->next;\n }\n\n return ret->next;\n }\n};", "memory": "138904" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* rmNode(ListNode* &head){\n if(head->next == NULL)\n return head;\n ListNode* temp = rmNode(head->next);\n if(head->val < temp->val){\n head->next = NULL;\n delete head;\n return temp;\n }\n head->next = temp;\n return head;\n }\n ListNode* removeNodes(ListNode* head) {\n return rmNode(head);\n }\n};", "memory": "141118" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n vector<ListNode*> v;\n ListNode *cur = head;\n while(cur) {\n while((int)v.size() > 0 && v.back()->val < cur->val) v.pop_back();\n v.push_back(cur);\n cur = cur->next;\n }\n head = NULL;\n if(v.size() > 0) head = v[0];\n for(int i = 0; i < v.size(); i++) {\n if(i == v.size() - 1) {\n v[i]->next = NULL;\n break;\n }\n v[i]->next = v[i + 1];\n }\n return head;\n }\n};", "memory": "141118" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(!head || !head->next) return head;\n stack<ListNode*> s;\n ListNode* curr = head;\n while(curr) {\n if(s.empty() || curr->val <= s.top()->val ) {\n s.push(curr);\n curr = curr->next;\n } else {\n s.pop();\n }\n }\n stack<ListNode*> rs;\n while(!s.empty()){\n rs.push(s.top());\n s.pop();\n }\n ListNode* ans = rs.top();\n ListNode* node = ans;\n rs.pop();\n while(!rs.empty()){\n node->next = rs.top();\n rs.pop();\n node=node->next;\n } \n node->next=NULL;\n return ans;\n }\n};", "memory": "143331" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(!head || !head->next) return head;\n stack<ListNode*> s;\n ListNode* curr = head;\n while(curr) {\n if(s.empty() || curr->val <= s.top()->val ) {\n s.push(curr);\n curr = curr->next;\n } else {\n s.pop();\n }\n }\n stack<ListNode*> rs;\n while(!s.empty()){\n rs.push(s.top());\n s.pop();\n }\n ListNode* ans = rs.top();\n ListNode* node = ans;\n rs.pop();\n while(!rs.empty()){\n node->next = rs.top();\n rs.pop();\n node=node->next;\n } \n node->next=NULL;\n return ans;\n }\n};\n", "memory": "143331" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if (!head || !head->next)\n return head;\n\n ListNode* nextNode = removeNodes(head->next);\n\n if (head->val < nextNode->val) {\n delete head;\n return nextNode;\n }\n\n head->next = nextNode;\n return head;\n }\n};\n", "memory": "145545" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if (!head || !head->next)\n return head;\n\n ListNode* nextNode = removeNodes(head->next);\n\n if (head->val < nextNode->val) {\n delete head;\n return nextNode;\n }\n\n head->next = nextNode;\n return head;\n }\n};\n", "memory": "145545" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if (!head || !head->next)\n return head;\n\n ListNode* nextNode = removeNodes(head->next);\n\n if (head->val < nextNode->val) {\n delete head;\n return nextNode;\n }\n\n head->next = nextNode;\n return head;\n }\n};\n", "memory": "147759" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(head==NULL || head->next==NULL) return head;\n\n ListNode* nxtNode = removeNodes(head->next);\n\n if(head->val < nxtNode->val){\n delete head;\n return nxtNode;\n }\n\n head->next = nxtNode;\n\n return head;\n }\n};", "memory": "147759" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\nprivate : \n ListNode* removeElements(ListNode* head, int val) {\n if(head == NULL) return head;\n ListNode *temp = head,*prev = NULL;\n while(temp != NULL){\n if(temp->val == val && temp == head) {\n head = head->next;\n temp = head;\n continue;\n }\n if(temp->val == val){\n (temp->next == NULL ? prev->next = NULL : prev->next = temp->next);\n temp = temp->next;\n continue;\n }\n prev = temp;\n temp = temp->next;\n }\n return head;\n }\npublic:\n ListNode* removeNodes(ListNode* head) {\n vector<int> nextFirstgreaters;\n ListNode *temp = head;\n while(temp != NULL){\n nextFirstgreaters.push_back(temp->val);\n temp = temp->next;\n }\n stack<int> stk;\n stk.push(INT_MAX);\n int n = nextFirstgreaters.size()-1;\n for(int i = n ; i > -1 ; i--){\n int curr = nextFirstgreaters[i];\n while(stk.top() <= curr){\n stk.pop();\n }\n nextFirstgreaters[i] = stk.top();\n stk.push(curr);\n }\n temp = head;\n for(int i = 0 ; i < n ; i++){\n if(nextFirstgreaters[i] != INT_MAX){\n temp->val = -1;\n }\n temp = temp->next;\n }\n head = removeElements(head,-1);\n return head;\n }\n};\n", "memory": "149973" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\ninline static auto _ = []() {\n cin.tie(0)->sync_with_stdio(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if (!head) return head;\n\n vector<int> largest;\n ListNode* temp = head;\n while (temp) {\n largest.push_back(temp -> val);\n temp = temp -> next;\n }\n\n for (int i = largest.size() - 2; i >= 0; i--) {\n largest[i] = max(largest[i], largest[i + 1]);\n }\n\n ListNode* dummy = new ListNode(0);\n dummy -> next = head;\n int i = 0;\n temp = dummy;\n while (i < largest.size() && temp -> next) {\n while (temp -> next && temp -> next -> val < largest[i]) {\n temp -> next = temp -> next -> next;\n i += 1;\n }\n temp = temp -> next;\n i += 1;\n }\n\n return dummy -> next;\n }\n};\n", "memory": "152186" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if (head->next == NULL) return head;\n vector<int> v;\n ListNode* temp = head;\n int i,maxi=0;\n while (temp != NULL) {\n v.push_back(temp->val);\n temp = temp->next;\n }\n for(i=v.size()-1;i>=0;i--){\n maxi=max(v[i],maxi);\n if(maxi>v[i]) v[i]=-1;\n }\n temp=head;\n for(i=0;i<v.size()-1;i++){\n if(v[i]!=-1){\n temp->val=v[i];\n temp=temp->next;\n }\n }\n temp->val=v[v.size()-1];\n temp->next=NULL;\n return head;\n }\n};", "memory": "154400" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if (!head) return head;\n\n vector<int> largest;\n ListNode* temp = head;\n while (temp) {\n largest.push_back(temp -> val);\n temp = temp -> next;\n }\n\n for (int i = largest.size() - 2; i >= 0; i--) {\n largest[i] = max(largest[i], largest[i + 1]);\n }\n\n ListNode* dummy = new ListNode(0);\n dummy -> next = head;\n int i = 0;\n temp = dummy;\n while (i < largest.size() && temp -> next) {\n while (temp -> next && temp -> next -> val < largest[i]) {\n temp -> next = temp -> next -> next;\n i += 1;\n }\n temp = temp -> next;\n i += 1;\n }\n\n return dummy -> next;\n }\n};\n", "memory": "156614" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n vector<int> v;\n ListNode* cur = head;\n while (cur) {\n v.push_back(cur->val);\n cur = cur->next;\n }\n int m = v.back();\n for (int i = v.size() - 1; 0 <= i; --i) {\n m = max(m, v[i]);\n v[i] = m;\n }\n ListNode** c = &head;\n int i = 0;\n while ((*c)->next) {\n if ((*c)->val < v[i + 1]) {\n (*c) = (*c)->next;\n } else {\n c = &((*c)->next);\n }\n ++i;\n }\n return head;\n }\n};\n", "memory": "158828" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(head==NULL || head->next==NULL)\n return head;\n vector<int>store;\n ListNode *temp=head;\n while(temp!=NULL)\n {\n store.push_back(temp->val);\n temp=temp->next;\n }\n stack<int>s;\n int n=store.size();\n s.push(store[n-1]);\n store[n-1]=0;\n for(int i=n-2;i>=0;i--)\n {\n while(!s.empty() && store[i]>=s.top())\n {\n s.pop();\n }\n if(s.empty())\n {\n s.push(store[i]);\n store[i]=0;\n }\n else\n {\n s.push(store[i]);\n store[i]=1;\n }\n }\n int ind=0;\n while(ind<store.size() && head->next!=NULL && store[ind]==1)\n {\n head=head->next;\n ind++;\n }\n if(head->next==NULL)\n return head;\n \n ind++;\n ListNode *pre=head,*cur=head->next;\n while(ind<store.size() && cur->next!=NULL)\n {\n if(store[ind]==1)\n {\n pre->next=cur->next;\n cur=cur->next;\n }\n else\n {\n pre=cur;\n cur=cur->next;\n }\n ind++;\n }\n return head;\n }\n};", "memory": "161041" }
2,573
<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>
2
{ "code": "class Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode *> st;\n ListNode *cur = head;\n\n while (cur) {\n st.push(cur);\n cur = cur->next;\n }\n cur = st.top();\n st.pop();\n while (!st.empty()) {\n if (st.top()->val >= cur->val) {\n st.top()->next = cur;\n cur = st.top();\n }\n st.pop();\n }\n\n return cur;\n }\n};", "memory": "163255" }
2,573
<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>
2
{ "code": "class Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode *> st;\n ListNode *cur = head;\n\n while (cur) {\n st.push(cur);\n cur = cur->next;\n }\n cur = st.top();\n st.pop();\n while (!st.empty()) {\n if (st.top()->val >= cur->val) {\n st.top()->next = cur;\n cur = st.top();\n }\n st.pop();\n }\n\n return cur;\n }\n};", "memory": "163255" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n public:\n ListNode* removeNodes(ListNode* head) {\n if(head->next == nullptr) return head;\n\n stack<ListNode*> s;\n\n while(head->next != nullptr) {\n s.push(head);\n head = head->next;\n }\n\n ListNode* newHead = head;\n int max_val = head->val;\n\n while(!s.empty()) {\n ListNode* topNode = s.top();\n if(topNode->val < max_val) {\n s.pop();\n }\n else {\n max_val = topNode->val;\n topNode->next = newHead;\n newHead = topNode;\n s.pop();\n }\n }\n\n return newHead;\n }\n};", "memory": "165469" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\nprivate:\n ListNode* reverse(ListNode* head) {\n if (head == nullptr) {\n return head;\n }\n ListNode* curr = head;\n ListNode* prev = nullptr;\n ListNode* next1 = nullptr;\n\n while (curr != nullptr) {\n next1 = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next1;\n }\n return prev;\n }\n\npublic:\n ListNode* removeNodes(ListNode* head) {\n if (head == nullptr) {\n return head;\n }\n ListNode* newHead = reverse(head);\n stack<int> st;\n ListNode* curr = newHead;\n while (curr != nullptr) {\n if (st.empty()) {\n st.push(curr->val);\n } else if (!st.empty() && curr->val >= st.top()) {\n st.push(curr->val);\n }\n curr = curr->next;\n }\n ListNode* newHead1 = new ListNode(st.top());\n ListNode* newTail = newHead1;\n st.pop();\n while (!st.empty()) {\n ListNode* temp = new ListNode(st.top());\n newTail->next = temp;\n newTail = newTail->next;\n st.pop();\n }\n return newHead1;\n }\n};", "memory": "174324" }
2,573
<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>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\nprivate:\n int removeNodesRec(ListNode* prev, ListNode* current){\n if(current->next == nullptr) return current->val;\n\n const int max_seen_so_far = removeNodesRec(current, current->next);\n const int val = current->val;\n\n if(val < max_seen_so_far) {\n prev->next = current->next;\n delete current;\n }\n\n return std::max(val, max_seen_so_far);\n }\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode new_head (10000000, head);\n removeNodesRec(&new_head, head); \n return new_head.next;\n }\n};", "memory": "174324" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n\n ListNode* reverse(ListNode* head)\n {\n ListNode* prev = NULL;\n ListNode* curr = head;\n ListNode* next = NULL;\n\n while(curr != NULL)\n {\n next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n return prev;\n }\n\n ListNode* removeNodes(ListNode* head) {\n ListNode* ans = NULL;\n ListNode* head2 = NULL;\n ListNode* temp = reverse(head);\n\n stack<ListNode*> st;\n while(temp != NULL)\n {\n if(st.empty())\n st.push(temp);\n else {\n if(temp->val >= st.top()->val)\n st.push(temp);\n }\n temp = temp->next;\n }\n while(!st.empty()) {\n ListNode* top = st.top();\n st.pop();\n if(ans == NULL) {\n ans = new ListNode(top->val);\n head2 = ans;\n }\n else {\n ans->next = new ListNode(top->val);\n ans = ans->next;\n }\n }\n return head2;\n }\n};", "memory": "176538" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n ListNode* ptr=head;\n ListNode* start=head;\n ListNode*temp=head;\n vector<int>v,t;\n while(temp!=NULL ){\n v.push_back(temp->val);\n temp=temp->next;\n }\n reverse(v.begin(),v.end());\n for(int i=0;i<v.size();i++)cout<<v[i]<<\" \";\n int min=v[0];\n t.push_back(min);\n for(int i=1;i<v.size();i++){\n if(v[i]>=min){\n t.push_back(v[i]);\n min=v[i];\n } \n }\n reverse(t.begin(),t.end());\n for(int i=0;i<t.size();i++)cout<<t[i]<<\" \";\n int n=t.size(),i=0;\n temp=head;\n while(temp!=NULL && i<n){\n if(t[i]==temp->val){\n if(temp!=head)ptr->next=temp;\n ptr=temp;\n if(i==0)start=temp;\n i++;\n }\n temp=temp->next;\n }\n return start;\n }\n};", "memory": "176538" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<int> st;\n ListNode* temp = head;\n\n while(temp != NULL){\n st.push(temp->val);\n temp = temp->next;\n }\n\n ListNode* dummyNode = new ListNode(-1);\n ListNode* dtemp = dummyNode;\n\n int maxi = INT_MIN;\n maxi = st.top();\n // dtemp->next = new ListNode(maxi);\n // dtemp = dtemp->next;\n\n while(!st.empty()){\n int top = st.top();\n if(top >= maxi){\n maxi = top;\n dtemp->next = new ListNode(maxi);\n dtemp = dtemp->next;\n }\n st.pop();\n }\n\n ListNode* newHead = dummyNode->next;\n dummyNode->next = NULL;\n delete dummyNode;\n\n // Reversing the prepared list\n temp = newHead;\n ListNode* prev = NULL;\n ListNode* front = NULL;\n\n while(temp != NULL){\n front = temp->next;\n temp->next = prev;\n prev = temp;\n temp = front;\n }\n\n return prev;\n }\n};", "memory": "178751" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<int>st;\n ListNode* temp = head;\n while(temp!=NULL){\n st.push(temp->val);\n temp=temp->next;\n }\n ListNode* current=new ListNode(st.top());\n st.pop();\n while(!st.empty()){\n int item = st.top();\n st.pop();\n if(item >= current->val){\n ListNode* New = new ListNode(item);\n New->next = current;\n current = New;\n }\n }\n return current;\n }\n};", "memory": "178751" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n#pragma GCC optimize(\"O3\", \"unroll-loops\")\n\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<pair<ListNode* ,int>> my_stack;\n ListNode *current = head;\n int index = 0;\n while(current){\n while(!my_stack.empty() && my_stack.top().first->val < current->val){\n my_stack.pop();\n }\n my_stack.push({current, index++});\n current = current->next;\n }\n index=0;\n current=nullptr;\n while(!my_stack.empty()){\n ListNode* node = my_stack.top().first;\n ListNode* tmp=node;\n node->next=current;\n current = tmp;\n my_stack.pop();\n }\n head = current;\n\n return head;\n }\n};", "memory": "180965" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<pair<int,ListNode*>> st;\n\n ListNode* temp = head;\n\n while(temp!=NULL){\n int value = temp->val;\n bool isDeleted =false;\n while(!st.empty() and value > st.top().first){\n st.pop();\n isDeleted=true;\n }\n if(!st.empty() and isDeleted){\n pair<int,ListNode*> node = st.top();\n node.second->next = temp;\n }\n if(st.empty() and isDeleted){\n head = temp;\n }\n st.push({value,temp});\n temp = temp->next;\n }\n\n return head;\n }\n};", "memory": "180965" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*> st;\n vector<int> v;\n ListNode* temp = head;\n while(temp){\n if(st.empty() || temp->val <= st.top()->val){\n st.push(temp);\n }\n else{\n while(!st.empty() && temp->val > st.top()->val){\n st.pop();\n }\n st.push(temp);\n }\n temp = temp->next;\n }\n while(!st.empty()){\n v.push_back(st.top()->val);\n st.pop();\n }\n reverse(v.begin(),v.end());\n temp = NULL;\n for(auto it:v){\n if(temp == NULL){\n ListNode* newNode = new ListNode(it);\n temp = newNode;\n head = temp;\n }\n else{\n ListNode* newNode = new ListNode(it);\n temp->next = newNode;\n temp = temp->next;\n }\n }\n return head;\n \n }\n};", "memory": "183179" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n stack<ListNode*>st;\n ListNode* temp=head;\n st.push(head);\n temp=temp->next;\n while(temp!=NULL){\n while(!st.empty() && temp->val > (st.top())->val) st.pop();\n st.push(temp);\n temp=temp->next;\n }\n if(st.size()==0) return NULL;\n else{\n stack<ListNode*>helper;\n while(!st.empty()){\n helper.push(st.top());\n st.pop();\n }\n ListNode* n=new ListNode(helper.top()->val);\n head=n;\n temp=head;\n helper.pop();\n while(!helper.empty()){\n n=new ListNode(helper.top()->val);\n temp->next=n;\n helper.pop();\n temp=temp->next;\n }\n return head;\n }\n }\n};", "memory": "183179" }
2,573
<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>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * struct ListNode *next;\n * };\n */\n\n /*\n1. Store the pointers in vector on purpose for simplicity.\n\n2. Traverse vector from right to left. This will help you identify nodes\n\nto the right larger than ones at the left.\n\nAs you traverse to the left keep track of the largest node found so far (max node value)\n\nIf a node is smaller than a node to the right do not prepend it to the new vector\n\nLink all nodes in vector and return head node. :)\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n\n vector<ListNode*> arr;\n\n for ( ListNode * r = head ; r != NULL ; r = r->next )\n {\n arr.push_back(r);\n \n }\n\n const size_t ans_size = arr.size();\n \n ListNode * ans[ans_size];\n\n memset(ans,0x00,ans_size*sizeof(ListNode*));\n\n size_t k = ans_size;\n\n int maxval = 0;\n\n for ( size_t i = arr.size() - 1 , j = 0 ; j < arr.size() ; i-- , j++ )\n {\n if ( arr[i]->val >= maxval ) \n {\n maxval = arr[i]->val;\n\n ans[--k] = arr[i];\n }\n\n }\n\n printf(\"%lu\\n\",k);\n\n for ( size_t i = k, j = k + 1 ; j < ans_size ; i++,j++ )\n {\n ans[i]->next = ans[j];\n }\n\n printf(\"Made it\");\n\n // ans[ans_size-1]->next = NULL;\n\n return ans[k];\n \n }\n};", "memory": "185393" }