"""
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. 开启Linux服务器vnc远程服务详细步骤

    1.安装rpm包 ,如果没有这个包,请提前下载好,然后输入命令安装   yum localinstall  /usr/local/tigervnc-server-1.1.0-24.el6.x86_64 ...

  2. Educational Codeforces Round 72 (Rated for Div. 2)C(暴力)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[200007];int a[20 ...

  3. Systemverilog for design 笔记(四)

    转载请标明出处 数组.结构体和联合体 1. 结构体(struct) 1.1. 结构体声明 结构体默认是变量,也可以声明为线网 var struct { // 通过var进行结构体变量声明 logic ...

  4. Linux centosVMware Linux集群架构LVS DR模式搭建、keepalived + LVS

    一.LVS DR模式搭建 三台机器 分发器,也叫调度器(简写为dir) davery :1.101 rs1 davery01:1.106 rs2 davery02:11.107 vip 133.200 ...

  5. 「国家集训队」Crash的数字表格

    题目描述 求(对 \(20101009\) 取模,\(n,m\le10^7\) ) \[\sum_{i=1}^n\sum_{j=1}^m\operatorname{lcm}(i,j)\] 大体思路 推 ...

  6. 1-1SpringBoot简介

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...

  7. keep-alive的使用

    <keep-alive>是Vue的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM. <router-view>中间为组件</router-view&g ...

  8. 详述ThreadLocal

    ThreadLocal的作用和目的:用于实现线程内的数据共享,即对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,而在另外线程中运行时又共享另外一份数据. 举一个反面例子,当我们使用简单 ...

  9. Oracle如何修改密码?如何解锁scott用户?

    修改密码: scott用户的密码不对,进入管理员用户,修改scott用户密码即可 或者这样修改密码:在运行栏里面敲:sqlplus(空格)/nolog 回车接着在弹出框里面敲:conn sys/pas ...

  10. PHP pclzip.php 解压中文乱码

    修改 pclzip中方法privExtractFile 代码 if ($p_path != '') { $p_entry['filename'] = $p_path."/".$p_ ...