389. Remove Nodes From Linked List (Monotonic Stack)

MediumLinked ListMonotonic StackLinked List

Given the head of a linked list, remove every node that has a node with a strictly greater value anywhere to its right. Return the head of the modified list as an array. (The result is a non-increasing sequence.)

Input: An array of node values.

Output: Array — the modified list.

Examples

Example 1
Input: [5,2,13,3,8]
Output: [13,8]
Explanation: 5,2,3 each have a greater node to the right.
Example 2
Input: [1,1,1,1]
Output: [1,1,1,1]
Explanation: No strictly greater node on the right.
Example 3
Input: [1,2,3,4,5]
Output: [5]
Explanation: Only the maximum remains.

Constraints

Asked by

AmazonMicrosoftGoogleMeta
Solve this problem in the editor →