"""
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. 3 JavaScript正则表达式

    正则表达式:Regular(有规则的) Expression 正则表达式是由一个字符序列形成的搜索模式,可用于文本搜索和文本替换 常见于字符串的search和replace方法 var str = & ...

  2. tensorflow版helloworld---拟合线性函数的k和b(02-4)

    给不明白深度学习能干什么的同学,感受下深度学习的power import tensorflow as tf import numpy as np #使用numpy生成100个随机点 x_data=np ...

  3. Kafka 启动报错java.io.IOException: Can't resolve address.

    阿里云上 部署Kafka 启动报错java.io.IOException: Can't resolve address. 本地调试的,报错 需要在本地添加阿里云主机的 host 映射   linux ...

  4. sql数据库的基本操作

    命令行 1.显示当前数据库服务器中的数据库列表:mysql> SHOW DATABASES;2.建立数据库:mysql> CREATE DATABASE 库名;3.建立数据表:mysql& ...

  5. 为U盘装备Ubuntu工作学习两不误

        题记: 在上一篇文章中,我介绍了让Ubuntu 10.04完美支持Thinkpad小红点Trackpoint.看上去,显得有些不痛不痒,实际上有些同学会因为小红点中键不能正常使用,而放弃在Th ...

  6. “嘭、嘭、嘭”---C/S架构下的心跳机制

    本人想使用AU3开发多客户端.一服务端.需要使用到心跳机制,即 在线状态实时更新以及掉线自动重连. 搜索网络发现没有人用AU3写心跳机制. 下面是一篇转帖(原文地址:http://www.cnblog ...

  7. LLDB常用指令

    1.help:列举所有的命令,也可以用于查询某个命令的说明,比如,help print,help help   2.print:打印,简写为,prin,pri,p,打印的结果比如,$10代表时该结果, ...

  8. CentOS6.9安装MySQL(编译安装、二进制安装)

    目录 CentOS6.9安装MySQL Linux安装MySQL的4种方式: 1. 二进制方式 特点:不需要安装,解压即可使用,不能定制功能 2. 编译安装 特点:可定制,安装慢 5.5之前: ./c ...

  9. Python print()函数

    #输出单个数据,会自动输出回车换行 print(1) print(2) #输出 1 2 #输出换行 print('\n') #防止换行 for x in range(0, 5): print(x, e ...

  10. 从POST和GET和request过滤掉SQL注入

    替换掉sql关键字,进行处理 // sql参数过滤 function sqlCheck($paramater){ $arr = array(); foreach($paramater as $k=&g ...