Ctrl K

Next Larger Down the List

Medium
You are given head, the head of a singly linked list of positive numbers. For each node, find the value of the first node after it whose value is strictly larger. If there is no such node, its answer is 0. Return the answers as an array: element i belongs to node i of the list (0-based).
Example 1
head=list_node([3, 8, 6, 2, 7]) → [8, 0, 7, 7, 0]

For 3 the first larger value further down is 8. Nothing beats 8, so it gets 0. Both 6 and 2 get 7. The last node gets 0.

Example 2
head=list_node([5, 5, 5]) → [0, 0, 0]

Equal values do not count as larger, so every answer is 0.

Example 3
head=list_node([1, 4, 9]) → [4, 9, 0]
Constraints
  • The list has 1 to 10⁵ nodes
  • 1 ≤ node value ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNights of the Withering Row MediumPreorder of a Search Tree MediumLow, Peak, Middle MediumNext Larger Around the Circle MediumFix the Middle of the Line Medium
loading Python…
Input
head=list_node([3, 8, 6, 2, 7])
Expected
[8, 0, 7, 7, 0]

Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.