首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Leetcode 1019. Next Greater Node In Linked List

Leetcode 1019. Next Greater Node In Linked List

作者头像
Tyan
发布2021-07-29 12:36:54
发布2021-07-29 12:36:54
4480
举报
文章被收录于专栏:SnailTyanSnailTyan

1. Description

2. Solution

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

  • Version 1
代码语言:javascript
复制
# 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/
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/07/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 1. Description
  • 2. Solution
  • Reference
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档