"""
We are given a linked list with head as the first node. Let's number the nodes in the list: node_1, node_2, node_3, ... etc. Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0. Return an array of integers answer, where answer[i] = next_larger(node_{i+1}). Note that in the example inputs (not outputs) below, arrays such as [2,1,5] represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5. Example 1: Input: [2,1,5]
Output: [5,5,0] Example 2: Input: [2,7,4,3,5]
Output: [7,0,5,5,0] Example 3: Input: [1,7,5,1,9,2,5,1]
Output: [7,9,9,9,0,5,0,0] """
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution1(object):
def nextLargerNodes(self, head):
if not head.next:
return head if head.val != 0 else None #判断头节点是为否为空
nums = []
p = head
while(p): #将链表转为数组
nums.append(p.val)
p = p.next
stack = [] #创建一个栈
res = [0] * len(nums) #保存结果的数组
#bug 0 没有加[0]
for i, n in enumerate(nums): #
while stack and nums[stack[-1]] < n: #!!!单调递减的栈,栈中存的是索引
res[stack.pop()] = n
stack.append(i) #将索引压栈
return res
"""
runtime error
单调递减(增)栈,是一个非常普遍的解法
传送门https://blog.csdn.net/qq_17550379/article/details/86519771
""" """
我们也可以不将链表中的元素存放到一个list里面,
而是直接去处理链表,不过对于链表我们无法快速索引具体位置的值,
所以我们可以在stack中记录(index, val)数据对。
"""
class Solution2(object):
def nextLargerNodes(self, head):
res, stack = list(), list()
while head:
while stack and stack[-1][1] < head.val:
res[stack.pop()[0]] = head.val
stack.append([len(res), head.val])
res.append(0)
head = head.next
return res

leetcode1019 Next Greater Node In Linked List的更多相关文章

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

    题目标签:Linked List, Stack 题目给了我们一个 Linked List,让我们找出对于每一个数字,它的下一个更大的数字. 首先把 Linked List 里的数字 存入 ArrayL ...

  2. [Swift]LeetCode1019. 链表中的下一个更大节点 | Next Greater Node In Linked List

    We are given a linked list with head as the first node.  Let's number the nodes in the list: node_1, ...

  3. 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...

  4. 【leetcode】1019. Next Greater Node In Linked List

    题目如下: We are given a linked list with head as the first node.  Let's number the nodes in the list: n ...

  5. Leetcode 1019. Next Greater Node In Linked List

    单调栈的应用. class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: stack = [] ret = ...

  6. Lintcode225-Find Node in Linked List-Naive

    225. Find Node in Linked List Find a node with given value in a linked list. Return null if not exis ...

  7. remove Nth Node from linked list从链表中删除倒数第n个元素

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. 算法与数据结构基础 - 链表(Linked List)

    链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. c++中的运算符重载operator1(翁恺c++公开课[30]学习笔记)

    运算符重载规则: 只有已经存在的运算符才能被重载,不能自己制造一个c++中没有的运算符进行重载 重载可以在类或枚举类型内进行,也可以是全局函数,但int.float这种已有的类型内是不被允许的 不能二 ...

  2. 第一次试水bof

    BOF come from https://www.jarvisoj.com/challenges nc pwn2.jarvisoj.com 9881(远程连接) 在ubuntu16.04上进行连接, ...

  3. angular 自定义服务封装自定义http请求

    在angular中将http请求,放置在一起封装成服务,可减少代码重复,方便使用 var ngpohttprest = angular.module('ngpohttprest', []); ngpo ...

  4. jqGrid不支持IE8的解决办法

    参考:https://blog.csdn.net/tarataotao/article/details/10376657

  5. display:flex下子元素宽度无效

    在子元素上设置: width:60px; flex-shrink:0;

  6. 百度统计数据导出demo的坑

    1.用户名中文的问题 由于demo文件格式的问题,如果用户名使用中文的话,会出现一下问题 ----------------------preLogin----------------------  [ ...

  7. 4.2 Scikit-Learn简介(机器学习篇)

    目录 第四章 机器学习 4.1 机器学习简介 4.1.1 机器学习分类 4.2 Scikit-Learn简介 4.2.1 Scikit-Learn的数据表示 4.2.2 Scikit-Learn的评估 ...

  8. stm32_f103使用gcc编译的环境下printf打印函数的实现

    前记   gcc编译使用的printf打印函数需要的底层函数是和其他编译器不同的,以前的是无法使用的,这里有两种方法,一种是使用gcc库里面的printf函数,自己实现底层IO函数_write.另外一 ...

  9. SpringBoot 入门demo

    创建SpringBoot项目方式一 (1)新建maven项目,不使用骨架. 使用maven管理依赖就行了,不必使用骨架(模板). (2)在pom.xml中添加 <!--springboot核心. ...

  10. 099、Java中String类之字符数组与字符串的转换

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...