383. Count Nodes Deleted (Greater Value on Right)

MediumLinked ListLinked ListReverse Traversal

Given a singly linked list, consider deleting every node that has a strictly greater value somewhere to its right. Return how many nodes would be deleted.

Input: An array of node values.

Output: Integer — number of nodes deleted.

Examples

Example 1
Input: [12,15,10,11,5,6,2,3]
Output: 4
Explanation: 4 nodes have a greater value on their right.
Example 2
Input: [10,20,30,40]
Output: 3
Explanation: All but the last are deleted.
Example 3
Input: [40,30,20,10]
Output: 0
Explanation: None deleted.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →