Leetcode 1019. Next Greater Node In Linked List

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1019. Next Greater Node In Linked List相关的知识,希望对你有一定的参考价值。

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

2. Solution

**解析:**Version 1,这个题跟Leetcode 503. Next Greater Element II非常相似,只不过是把数组换成了链表,参考https://blog.csdn.net/Quincuntial/article/details/118733487即可。

  • Version 1
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def nextLargerNodes(self, head: ListNode) -> List[int]:
        result = []
        nums = []
        node = head
        index = 0
        stack = []
        while node:
            while stack and nums[stack[-1]] < node.val:
                result[stack.pop()] = node.val
            nums.append(node.val)
            result.append(0)
            stack.append(index)
            node = node.next
            index += 1
        return result

Reference

  1. https://leetcode.com/problems/next-greater-node-in-linked-list/

以上是关于Leetcode 1019. Next Greater Node In Linked List的主要内容,如果未能解决你的问题,请参考以下文章

[栈] leetcode 1019 Next Greater Node In Linked List

Leetcode 1019. Next Greater Node In Linked List

Leetcode 1019. Next Greater Node In Linked List

[LeetCode] 1019. Next Greater Node In Linked List

LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)

Leetcode 87. Scramble String