367. Delete Nodes Having Greater Value on Right

MediumLinked ListLinked ListReverse Traversal

Given a singly linked list, delete every node that has a node with a strictly greater value anywhere to its right. Return the resulting list as an array.

Input: An array of node values.

Output: Array — the list after deletions.

Examples

Example 1
Input: [12,15,10,11,5,6,2,3]
Output: [15,11,6,3]
Explanation: Only nodes with no greater value on the right survive.
Example 2
Input: [10,20,30,40]
Output: [40]
Explanation: Each is dominated by a later node.
Example 3
Input: [40,30,20,10]
Output: [40,30,20,10]
Explanation: Already decreasing.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →