单调栈的应用.
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
原文地址:https://www.cnblogs.com/zywscq/p/10739575.html
时间: 2024-10-30 11:49:53