Next Greater Node In Linked List

You are given the head of a linked list with n nodes.

For each node in the list, find the value of the next greater node. That is, for each node, find the value of the first node that is next to it and has a strictly larger value than it.

Return an integer array answer where answer[i] is the value of the next greater node of the i^th node (1-indexed). If the i^th node does not have a next greater node, set answer[i] = 0.

Example 1
[2] -> [1] -> [5] -> null
Inputhead = [2,1,5]
Output[5,5,0]
For the nodes with values 2 and 1, the next greater value is 5; the node with value 5 has no next greater node.
Example 2
[2] -> [7] -> [4] -> [3] -> [5] -> null
Inputhead = [2,7,4,3,5]
Output[7,0,5,5,0]
The next greater values for nodes 2, 7, 4, 3, and 5 are 7, 0, 5, 5, and 0 respectively.

Constraints

  • The number of nodes in the list is n.
  • 1 <= n <= 10^4
  • 1 <= Node.val <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate