> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Remove Nodes From Linked List Python Solution

> Tested Python solution for LeetCode 2487 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2487, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list), [Stack](/catalog/topics/stack), [Recursion](/catalog/topics/recursion), [Monotonic Stack](/catalog/topics/monotonic-stack). [View on LeetCode](https://leetcode.com/problems/remove-nodes-from-linked-list/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2487   # by problem number
lcpy gen -s remove_nodes_from_linked_list   # by problem name
```

## Problem

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

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

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

### Examples

![Example 1](https://assets.leetcode.com/uploads/2022/10/02/drawio.png)

```
Input: head = [5,2,13,3,8]
Output: [13,8]
Explanation: 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.
```

```
Input: head = [1,1,1,1]
Output: [1,1,1,1]
Explanation: Every node has value 1, so no nodes are removed.
```

### Constraints

* The number of the nodes in the given list is in the range `[1, 10^5]`.
* `1 <= Node.val <= 10^5`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_nodes_from_linked_list/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_nodes_from_linked_list/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n)
    # Space: O(1)
    def remove_nodes(self, head: ListNode[int] | None) -> ListNode[int] | None:
        # Reverse the list so that "greater to the right" becomes "greater already kept".
        prev: ListNode[int] | None = None
        node = head
        while node is not None:
            nxt = node.next
            node.next = prev
            prev = node
            node = nxt

        cur = prev
        while cur is not None:
            nxt = cur.next
            if nxt is None:
                break
            if nxt.val < cur.val:
                cur.next = nxt.next
            else:
                cur = nxt

        # Reverse back to restore left-to-right order.
        result: ListNode[int] | None = None
        node = prev
        while node is not None:
            nxt = node.next
            node.next = result
            result = node
            node = nxt
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.