Skip to main content
LeetCode 2487, Medium. Topics: Linked List, Stack, Recursion, Monotonic Stack. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026