You are given a singly linked list with n nodes. For each node, find the value of the first node after it whose value is strictly greater than its own. Return a list answer where answer[i] is that value for the node at position i (counting from 0 at the head), or 0 when no later node has a greater value.
Function Signature
def next_larger_nodes(values: list[int]) -> list[int]:
Rules
-
The linked list is passed as a Python list of its node values, in order from the head.
values[0]
is the head node.
-
The next greater node of position
i
is the nearest position
j > i
with
values[j] > values[i]
. A later node with an equal value does not count.
-
The returned list has the same length as the input, and
answer[i]
belongs to the node at position
i
.
Constraints
-
The number of nodes
n
satisfies
1 <= n <= 10^4
.
-
Every node value satisfies
1 <= values[i] <= 10^9
.
Examples
Input: values = [2, 1, 5]
Output: [5, 5, 0]
After 2 the next value is 1, which is smaller, so the answer for 2 is 5. The node after 1 is 5, which is larger. Nothing follows 5, so its entry is 0.
Input: values = [2, 7, 4, 3, 5]
Output: [7, 0, 5, 5, 0]
No later value exceeds 7, so its entry is 0. Both 4 and 3 find 5 as the first larger value to their right.
Input: values = [3, 3, 4, 1, 2]
Output: [4, 4, 0, 2, 0]
The second 3 equals the first, which does not count as greater, so both 3s get 4. After 4 only 1 and 2 remain, both smaller, and 1 finds 2 right after it.