Remove Nodes From Linked List

You are given the head of a linked list. Remove every node which has a node with a greater value anywhere to the right side of it. Return the head of the modified linked list.

Constraints

  • The number of the nodes in the given list is in the range [1, 10^5].
  • 1 <= Node.val <= 10^5
Saving… · 9 lines
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next

class Solution:
def removeNodesWithGreaterValues(self, head: ListNode) -> ListNode:

Console

Click Run to execute sample tests.