1019. 链表中的下一个更大节点
思路:用栈记录当前较小元素的下标。 时间超越100%的python提交用户,空间超越62%的Python提交用户
1 # Definition for singly-linked list. 2 # class ListNode(object): 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution(object): 8 def nextLargerNodes(self, head): 9 if not head: 10 return [] 11 nums=[] 12 while head: 13 nums.append(head.val) 14 head=head.next 15 stacks,res=[],[0]*len(nums) 16 for i in range(len(nums)): 17 while stacks and nums[i]>nums[stacks[-1]]: 18 res[stacks.pop()]=nums[i] 19 stacks.append(i) 20 return res