[栈] 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 *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> nextLargerNodes(ListNode* head) {
        stack<pair<int,int>> sta;
        vector<int> res;
        int i = 0;
        ListNode* cur = head;
        while(cur)
        {
            while(!sta.empty() && cur->val > sta.top().first)
            {
                int idx = sta.top().second;
                sta.pop();
                res[idx] = cur->val;
            }
            sta.push({ cur->val, i++});
            res.push_back(0);
            cur = cur->next;
        }

        return res;
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11335303.html

时间: 2024-08-30 07:44:07

[栈] 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

单调栈的应用. 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

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 503 Next Greater Element II

problem:https://leetcode.com/problems/next-greater-element-ii/ 一道比较简单的单调队列题目.不过由于题目要求是循环的,需要两个pass,第二个pass处理循环生效的next greater,同时需要把下标已经超出范围的队首数据及时pop出来. class Solution { public: vector<int> nextGreaterElements(vector<int>& nums) { deque<

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