# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def nextLargerNodes(self, head):
"""
:type head: ListNode
:rtype: List[int]
"""
stack = []
if head == None:
return None
p = head
q = head
i = 0
while p:
i += 1
p = p.next
ans = [0]*i
j = 0
while q is not None:
while stack and stack[-1][0]<q.val:
ans[stack[-1][1]] = q.val
stack.pop()
stack.append((q.val,j))
j += 1
q = q.next
return ans
每日一道Leetcode - 1019. 链表中的下一个更大节点 【栈】
最新推荐文章于 2021-04-12 21:07:30 发布