1. 程式人生 > >Leetcode 1019. Next Greater Node In Linked List

Leetcode 1019. Next Greater Node In Linked List

tco self lee val tac des and link div

單調棧的應用.

class Solution:
    def nextLargerNodes(self, head: ListNode) -> List[int]:
        stack = []
        ret = []
        while head:
            while stack and stack[-1][1] < head.val:
                ret[stack.pop()[0]] = head.val
            stack.append((len(ret), head.val))
            ret.append(0)
            head 
= head.next return ret

Leetcode 1019. Next Greater Node In Linked List