Next Greater Node In Linked List

You are given the head of a linked list with n nodes. For each node, find the value of the next greater node, which is the first node following it with a strictly larger value. Return an array where each element is the value of the next greater node of the ith node. If there is no such node, use 0.

Constraints

  • The number of nodes in the list is n.
  • 1 <= n <= 10^4
  • 1 <= Node.val <= 10^9
Saving… · 9 lines
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next

class Solution:
def nextGreaterNode(self, head: ListNode) -> List[int]:

Console

Click Run to execute sample tests.