Problem statement

https://leetcode.com/problems/next-greater-node-in-linked-list/

Solution

Let us just put all elements in nums and then reuse classical next greater element problem with monotone decreasing stack. Or when we traverse we can put elements on the flight.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def nextLargerNodes(self, head):
        nums, stack = [], []
        while head:
            nums += [head.val]
            head = head.next
            
        n = len(nums)
        result = [0]*n
        
        for i, num in enumerate(nums):
            while stack and stack[-1][1] < num:
                a, b = stack.pop()
                result[a] = num
            stack.append((i, num))
            
        return result