题目如下:

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_inext_larger(node_i) is the node_j.val such that j > inode_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]

Note:

  1. 1 <= node.val <= 10^9 for each node in the linked list.
  2. The given list has length in the range [0, 10000].

解题思路:本题是找出离自己最近的大于自己的数,和以前做过的 【leetcode】84. Largest Rectangle in Histogram 非常相似,区别在于【leetcode】84. Largest Rectangle in Histogram 是找出最近的比自己小的数,但是原理是一样的。我的解法就是把链表转成list,然后参照【leetcode】84. Largest Rectangle in Histogram的解法。

代码如下:

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def nextLargerNodes(self, head):
"""
:type head: ListNode
:rtype: List[int]
"""
val = []
while head != None:
val.append(head.val)
head = head.next
res = [0] * len(val)
for i in range(len(val)-2,-1,-1):
next = i+1
while res[next] != 0 and val[i] >= val[next] :
next = res[next]
if val[i] >= val[next]:
res[i] = 0
else:
res[i] = next
#print res
for i in range(len(res)):
if res[i] == 0:
continue
res[i] = val[res[i]]
return res

【leetcode】1019. Next Greater Node In Linked List的更多相关文章

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

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

  2. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  3. 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)

    [LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

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

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

  5. 【LeetCode】496. Next Greater Element I 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接遍历查找 字典保存位置 日期 题目地址:http ...

  6. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  7. 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)

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

  8. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  9. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

随机推荐

  1. gitlab+jenkins自动化打包IOS-jenkins配置

    实现的效果如图:  构建界面: 完成效果: 功能说明: 根据选择的代码分支,执行构建打包 构建成功后根据ipa/apk生成二维码,并可在历史构建列表中展示各个版本的二维码,通过手机扫描二维码可直接安装 ...

  2. day16—正是Github,让社会化编程成为现实。

    转行学开发,代码100天——2018-04-01 今天简单了解了一下GitHub的使用. 对于GitHub,在很多年前开始写程序的时候就频繁听到,也早早地注册之后看了真容.但是由于自己一直未产出较大型 ...

  3. 从零开始创建 symfony-cmf

    前提: 官方 https://symfony.com/doc/master/cmf/quick_tour/the_big_picture.html#setting-up-the-database 由于 ...

  4. Altium Designer(AD)使用笔记

    在PCB中间打洞,螺丝孔等 制作PCB螺丝孔 1 在Keepout层首先绘制一个圆形(矩形): 2 在绘制PCB时,选中该图形,Tool>>Convert>>create bo ...

  5. jQuery 动态添加、删除css样式

    1.addClass css中: <style type="text/css">       .chColor {background: #267cb7;color:w ...

  6. FastReport使用教程

    FastReport使用心得 一.准备 1.这次开发使用的是FastReport桌面版(FastReport.Net Version 201731.16 Demo) 2.引用类库FastReport. ...

  7. Python 学习笔记18 异常处理

    我们在编码的过程中,难免会遇到一些错误和异常, 这时候程序会异常退出,并且会抛出错误信息: 比如: print(1/0) ''' 输出: Traceback (most recent call las ...

  8. knn算法手写字识别案例

    import pandas as pd import numpy as np import matplotlib.pyplot as plt import os from sklearn.neighb ...

  9. JS原型链详解

    最近面试被问到了就决定好好深入理解原型链 对象 要清楚原型链,首先要弄清楚对象: 普通对象 最普通的对象:有__proto__属性(指向其原型链),没有prototype属性. 原型对象(person ...

  10. Zookeeper---作为服务注册中心

    认识Zookeeper是一套分布式协调服务. 优点: 简单:与文件系统类似,Znode的组织方式. 多副本:一般再线上都是三副本或者五副本的形式,最少会有三个节点. 有序:有序的操作,根据时间戳进行排 ...