Remove Nodes From Linked List

Medium

Topics
Linked ListStackRecursionMonotonic Stack

Given the head of a linked list, remove every node which has a node with a strictly greater value anywhere to its right. Return the head of the modified list.

Example 1

Input:  head = [5,2,13,3,8]
Output: [13,8]

Example 2

Input:  head = [1,1,1,1]
Output: [1,1,1,1]

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^5
Run ⌘' · Submit ⌘⏎