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         vector<int> v;
13         ListNode *p = head;
14         ListNode *q = p;
15         if(head && head->val==10000)
16         {
17             if(p) p = p->next;
18             else return v;
19             v.push_back(0);
20         }
21         while(p)
22         {
23             q = p;
24             while(q && q->val<=p->val)
25                 q = q->next;
26             if(!q)
27             {
28                 v.push_back(0);
29             }
30             else
31                 v.push_back(q->val);
32             p = p->next;
33
34         }
35         return v;
36     }
37 };

原文地址:https://www.cnblogs.com/Asurudo/p/10630807.html

时间: 2024-08-12 22:51:08

Leetcode-1030 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 496. 下一个更大元素 I(Next Greater Element I) 35

496. 下一个更大元素 I 496. Next Greater Element I 题目描述 给定两个没有重复元素的数组?nums1 和?nums2,其中 nums1?是?nums2?的子集.找到?nums1?中每个元素在?nums2?中的下一个比其大的值. nums1?中数字?x?的下一个更大元素是指?x?在?nums2?中对应位置的右边的第一个比?x?大的元素.如果不存在,对应位置输出 -1. 每日一算法2019/6/7Day 35LeetCode496. Next Greater Ele

LeetCode 556. 下一个更大元素 III(Next Greater Element III)

556. 下一个更大元素 III 556. Next Greater Element III 题目描述 给定一个 32 位正整数 n,你需要找到最小的 32 位整数,其与 n 中存在的位数完全相同,并且其值大于 n.如果不存在这样的 32 位整数,则返回-1. LeetCode556. Next Greater Element III中等 示例 1: 输入: 12 输出: 21 示例 2: 输入: 21 输出: -1 示例 3: 输入: 12443322 输出: 13222344 Java 实现

503. Next Greater Element II 下一个更大元素

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

Leetcode 556.下一个更大元素III

下一个更大元素III 给定一个32位正整数 n,你需要找到最小的32位整数,其与 n 中存在的位数完全相同,并且其值大于n.如果不存在这样的32位整数,则返回-1. 示例 1: 输入: 12 输出: 21 示例 2: 输入: 21 输出: -1 C++: using next permutation int nextGreaterElement(int n) { auto digits = to_string(n); next_permutation(begin(digits), end(dig

[Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the ar

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

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

496. 求下一个更大的元素 Next Greater Element I

You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. The Next Greater Number of a number x in nums1 is t