Remove every node that has a node with a strictly greater value somewhere to its right, and return the modified list.
Given the head of a singly linked list, delete every node that has a node with a strictly larger value somewhere later in the list.
Keep only the nodes that are not dominated by a greater value on their right, and return the head of the remaining list.
Input Format
- A singly linked list represented by its head node.
- Each node contains an integer value.
Output Format
- Return the head of the linked list after removing all dominated nodes.
Constraints
- The list length is at least 1.
- Node values are integers.
- The answer should preserve the original relative order of the remaining nodes.
Example 1
Input
head = [5,2,13,3,8]
Output
[13,8]
Explanation
Node 5 is removed because 13 is greater on its right. Node 2 is removed because 13 is greater on its right. Node 13 stays. Node 3 is removed because 8 is greater on its right. Node 8 stays.
Example 2
Input
head = [1,1,1]
Output
[1,1,1]
Explanation
No node has a strictly greater value to its right, so all nodes remain.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.