任务描述
任务描述
- 维护一个单调栈
代码实现
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def nextLargerNodes(self, head: ListNode) -> List[int]:
map = {}
a = []
stack = []
ans = []
while head:
a.append(head)
head = head.next
for i in a[::-1]:
# print(i.val)
while len(stack)!=0 and stack[-1]<=i.val:
stack.pop()
if not stack:
map[i] = 0
else:
map[i] = stack[-1]
stack.append(i.val)
for i in a:
# print(i,map[i])
ans.append(map[i])
return ans