Design Gurus Logo
Remove Nodes From Linked List (easy)

Problem Statement

Given the head node of a singly linked list, modify the list such that any node that has a node with a greater value to its right gets removed. The function should return the head of the modified list.

Examples:

  1. Input: 5 -> 3 -> 7 -> 4 -> 2 -> 1
    Output: 7 -> 4 -> 2 -> 1
    Explanation: 5 and 3 are removed as they have nodes with larger values to their right.

  2. Input: 1 -> 2 -> 3 -> 4 -> 5
    Output: 5
    Explanation: 1, 2, 3, and 4 are removed as they have nodes with larger values to their right.

  3. Input: 5 -> 4 -> 3 -> 2 -> 1
    Output: 5 -> 4 -> 3 -> 2 -> 1
    Explanation: None of the nodes are removed as none of them have nodes with larger values to their right.

Constraints:

  • The number of the nodes in the given list is in the range [1, 10<sup>5</sup>].
  • 1 <= Node.val <= 10<sup>5</sup>

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory