379. Next Greater Node in Linked List

MediumLinked ListMonotonic StackLinked List

Given a linked list, for each node return the value of the first node to its right that has a strictly greater value, or 0 if there is no such node. Return the answers as an array in node order.

Input: An array of node values.

Output: Array — next greater value per node (0 if none).

Examples

Example 1
Input: [2,1,5]
Output: [5,5,0]
Explanation: Next greater of 2 and 1 is 5; 5 has none.
Example 2
Input: [2,7,4,3,5]
Output: [7,0,5,5,0]
Explanation: Per-node next greater.
Example 3
Input: [1]
Output: [0]
Explanation: No greater node.

Constraints

Asked by

MicrosoftBloombergAmazonGoogle
Solve this problem in the editor →