Leetcode 1019. Next Greater Node In Linked List

单调栈的应用.

class Solution:
    def nextLargerNodes(self, head: ListNode) -> List[int]:
        stack = []
        ret = []
        while head:
            while stack and stack[-1][1] < head.val:
                ret[stack.pop()[0]] = head.val
            stack.append((len(ret), head.val))
            ret.append(0)
            head = head.next
        return ret

原文地址:https://www.cnblogs.com/zywscq/p/10739575.html

时间: 2024-10-30 11:49:53

Leetcode 1019. Next Greater Node In Linked List的相关文章

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

题目标签:Linked List, Stack 题目给了我们一个 Linked List,让我们找出对于每一个数字,它的下一个更大的数字. 首先把 Linked List 里的数字 存入 ArrayList, 方便后面的操作. 然后遍历 ArrayList,首先每一个数字,都会存入stack:所以就可以利用stack回到之前的数字,存入它的 next Greater Node. Java Solution: Runtime:  39 ms, faster than 65 % Memory Usa

[栈] leetcode 1019 Next Greater Node In Linked List

problem:https://leetcode.com/problems/next-greater-node-in-linked-list/ 维护递减的单调栈.这道题对象是链表,不像数组可以快速通过下标索引,所以比较方便的做法是在栈中同时记录数字和对应的下标,并且默认填0,如果找到了比它大的第一个数,再修改下标对应的数字. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *ne

leetcode1019 Next Greater Node In Linked List

1 """ 2 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. 3 4 Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j &

Leetcode-1030 Next Greater Node In Linked List(链表中的下一个更大节点)

最后一个样例是特判过的 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 vector<int> nextLargerNodes(ListNode* head) { 12

LeetCode:Remove Nth Node From End of List

1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3.题目内容 英文:Given a linked list, remove the nth node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如:

LeetCode OJ - Remove Nth Node From End of List

题目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Give

[LeetCode 题解]: Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

[Leetcode][Tree][Flatten Binary Tree to Linked List ]

按照前序遍历的顺序把树用right连起来. 本来想了半天,一点思路都没有,总觉得Inplace的解法一般都非常巧妙. 后来我突发灵感,决定用一个变量记录当前访问到哪个点,真是太机智了~~ 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), r

【leetcode刷题笔记】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt