Skip to content

Commit 8d29144

Browse files
committed
feat: add solutions to lc problem: No.1019
No.1019.Next Greater Node In Linked List
1 parent 01d4e46 commit 8d29144

File tree

6 files changed

+297
-70
lines changed

6 files changed

+297
-70
lines changed

solution/1000-1099/1019.Next Greater Node In Linked List/README.md

+111-24
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49-
“单调栈”实现。
49+
**方法一:单调栈**
50+
51+
我们先遍历链表,将链表中的值存入数组 `nums` 中。
52+
53+
然后从后往前遍历数组 `nums`,维护一个从栈底到栈顶单调递减的栈 `stk`,遍历过程中,如果栈顶元素小于等于当前元素,则将栈顶元素出栈,直到栈顶元素大于当前元素或者栈为空。
54+
55+
如果栈为空,则说明当前元素没有下一个更大的元素,否则当前元素的下一个更大的元素就是栈顶元素,更新答案数组 `ans`
56+
57+
然后将当前元素入栈,继续遍历。
58+
59+
遍历结束后,返回答案数组 `ans` 即可。
60+
61+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
5062

5163
<!-- tabs:start -->
5264

@@ -57,24 +69,25 @@
5769
```python
5870
# Definition for singly-linked list.
5971
# class ListNode:
60-
# def __init__(self, x):
61-
# self.val = x
62-
# self.next = None
63-
64-
72+
# def __init__(self, val=0, next=None):
73+
# self.val = val
74+
# self.next = next
6575
class Solution:
66-
def nextLargerNodes(self, head: ListNode) -> List[int]:
76+
def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]:
6777
nums = []
6878
while head:
6979
nums.append(head.val)
7080
head = head.next
71-
s = []
72-
larger = [0] * len(nums)
73-
for i, num in enumerate(nums):
74-
while s and nums[s[-1]] < num:
75-
larger[s.pop()] = num
76-
s.append(i)
77-
return larger
81+
stk = []
82+
n = len(nums)
83+
ans = [0] * n
84+
for i in range(n - 1, -1, -1):
85+
while stk and stk[-1] <= nums[i]:
86+
stk.pop()
87+
if stk:
88+
ans[i] = stk[-1]
89+
stk.append(nums[i])
90+
return ans
7891
```
7992

8093
### **Java**
@@ -87,26 +100,100 @@ class Solution:
87100
* public class ListNode {
88101
* int val;
89102
* ListNode next;
90-
* ListNode(int x) { val = x; }
103+
* ListNode() {}
104+
* ListNode(int val) { this.val = val; }
105+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
91106
* }
92107
*/
93108
class Solution {
94109
public int[] nextLargerNodes(ListNode head) {
95110
List<Integer> nums = new ArrayList<>();
96-
while (head != null) {
111+
for (; head != null; head = head.next) {
97112
nums.add(head.val);
98-
head = head.next;
99113
}
100-
Deque<Integer> s = new ArrayDeque<>();
101-
int[] larger = new int[nums.size()];
102-
for (int i = 0; i < nums.size(); ++i) {
103-
while (!s.isEmpty() && nums.get(s.peek()) < nums.get(i)) {
104-
larger[s.pop()] = nums.get(i);
114+
Deque<Integer> stk = new ArrayDeque<>();
115+
int n = nums.size();
116+
int[] ans = new int[n];
117+
for (int i = n - 1; i >= 0; --i) {
118+
while (!stk.isEmpty() && stk.peek() <= nums.get(i)) {
119+
stk.pop();
120+
}
121+
if (!stk.isEmpty()) {
122+
ans[i] = stk.peek();
123+
}
124+
stk.push(nums.get(i));
125+
}
126+
return ans;
127+
}
128+
}
129+
```
130+
131+
### **C++**
132+
133+
```cpp
134+
/**
135+
* Definition for singly-linked list.
136+
* struct ListNode {
137+
* int val;
138+
* ListNode *next;
139+
* ListNode() : val(0), next(nullptr) {}
140+
* ListNode(int x) : val(x), next(nullptr) {}
141+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
142+
* };
143+
*/
144+
class Solution {
145+
public:
146+
vector<int> nextLargerNodes(ListNode* head) {
147+
vector<int> nums;
148+
for (; head; head = head->next) {
149+
nums.push_back(head->val);
150+
}
151+
stack<int> stk;
152+
int n = nums.size();
153+
vector<int> ans(n);
154+
for (int i = n - 1; ~i; --i) {
155+
while (!stk.empty() && stk.top() <= nums[i]) {
156+
stk.pop();
157+
}
158+
if (!stk.empty()) {
159+
ans[i] = stk.top();
160+
105161
}
106-
s.push(i);
162+
stk.push(nums[i]);
107163
}
108-
return larger;
164+
return ans;
109165
}
166+
};
167+
```
168+
169+
### **Go**
170+
171+
```go
172+
/**
173+
* Definition for singly-linked list.
174+
* type ListNode struct {
175+
* Val int
176+
* Next *ListNode
177+
* }
178+
*/
179+
func nextLargerNodes(head *ListNode) []int {
180+
nums := []int{}
181+
for ; head != nil; head = head.Next {
182+
nums = append(nums, head.Val)
183+
}
184+
stk := []int{}
185+
n := len(nums)
186+
ans := make([]int, n)
187+
for i := n - 1; i >= 0; i-- {
188+
for len(stk) > 0 && stk[len(stk)-1] <= nums[i] {
189+
stk = stk[:len(stk)-1]
190+
}
191+
if len(stk) > 0 {
192+
ans[i] = stk[len(stk)-1]
193+
}
194+
stk = append(stk, nums[i])
195+
}
196+
return ans
110197
}
111198
```
112199

solution/1000-1099/1019.Next Greater Node In Linked List/README_EN.md

+98-23
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,25 @@
4343
```python
4444
# Definition for singly-linked list.
4545
# class ListNode:
46-
# def __init__(self, x):
47-
# self.val = x
48-
# self.next = None
49-
50-
46+
# def __init__(self, val=0, next=None):
47+
# self.val = val
48+
# self.next = next
5149
class Solution:
52-
def nextLargerNodes(self, head: ListNode) -> List[int]:
50+
def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]:
5351
nums = []
5452
while head:
5553
nums.append(head.val)
5654
head = head.next
57-
s = []
58-
larger = [0] * len(nums)
59-
for i, num in enumerate(nums):
60-
while s and nums[s[-1]] < num:
61-
larger[s.pop()] = num
62-
s.append(i)
63-
return larger
55+
stk = []
56+
n = len(nums)
57+
ans = [0] * n
58+
for i in range(n - 1, -1, -1):
59+
while stk and stk[-1] <= nums[i]:
60+
stk.pop()
61+
if stk:
62+
ans[i] = stk[-1]
63+
stk.append(nums[i])
64+
return ans
6465
```
6566

6667
### **Java**
@@ -71,29 +72,103 @@ class Solution:
7172
* public class ListNode {
7273
* int val;
7374
* ListNode next;
74-
* ListNode(int x) { val = x; }
75+
* ListNode() {}
76+
* ListNode(int val) { this.val = val; }
77+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
7578
* }
7679
*/
7780
class Solution {
7881
public int[] nextLargerNodes(ListNode head) {
7982
List<Integer> nums = new ArrayList<>();
80-
while (head != null) {
83+
for (; head != null; head = head.next) {
8184
nums.add(head.val);
82-
head = head.next;
8385
}
84-
Deque<Integer> s = new ArrayDeque<>();
85-
int[] larger = new int[nums.size()];
86-
for (int i = 0; i < nums.size(); ++i) {
87-
while (!s.isEmpty() && nums.get(s.peek()) < nums.get(i)) {
88-
larger[s.pop()] = nums.get(i);
86+
Deque<Integer> stk = new ArrayDeque<>();
87+
int n = nums.size();
88+
int[] ans = new int[n];
89+
for (int i = n - 1; i >= 0; --i) {
90+
while (!stk.isEmpty() && stk.peek() <= nums.get(i)) {
91+
stk.pop();
8992
}
90-
s.push(i);
93+
if (!stk.isEmpty()) {
94+
ans[i] = stk.peek();
95+
}
96+
stk.push(nums.get(i));
9197
}
92-
return larger;
98+
return ans;
9399
}
94100
}
95101
```
96102

103+
### **C++**
104+
105+
```cpp
106+
/**
107+
* Definition for singly-linked list.
108+
* struct ListNode {
109+
* int val;
110+
* ListNode *next;
111+
* ListNode() : val(0), next(nullptr) {}
112+
* ListNode(int x) : val(x), next(nullptr) {}
113+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
114+
* };
115+
*/
116+
class Solution {
117+
public:
118+
vector<int> nextLargerNodes(ListNode* head) {
119+
vector<int> nums;
120+
for (; head; head = head->next) {
121+
nums.push_back(head->val);
122+
}
123+
stack<int> stk;
124+
int n = nums.size();
125+
vector<int> ans(n);
126+
for (int i = n - 1; ~i; --i) {
127+
while (!stk.empty() && stk.top() <= nums[i]) {
128+
stk.pop();
129+
}
130+
if (!stk.empty()) {
131+
ans[i] = stk.top();
132+
133+
}
134+
stk.push(nums[i]);
135+
}
136+
return ans;
137+
}
138+
};
139+
```
140+
141+
### **Go**
142+
143+
```go
144+
/**
145+
* Definition for singly-linked list.
146+
* type ListNode struct {
147+
* Val int
148+
* Next *ListNode
149+
* }
150+
*/
151+
func nextLargerNodes(head *ListNode) []int {
152+
nums := []int{}
153+
for ; head != nil; head = head.Next {
154+
nums = append(nums, head.Val)
155+
}
156+
stk := []int{}
157+
n := len(nums)
158+
ans := make([]int, n)
159+
for i := n - 1; i >= 0; i-- {
160+
for len(stk) > 0 && stk[len(stk)-1] <= nums[i] {
161+
stk = stk[:len(stk)-1]
162+
}
163+
if len(stk) > 0 {
164+
ans[i] = stk[len(stk)-1]
165+
}
166+
stk = append(stk, nums[i])
167+
}
168+
return ans
169+
}
170+
```
171+
97172
### **JavaScript**
98173

99174
```js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
vector<int> nextLargerNodes(ListNode* head) {
14+
vector<int> nums;
15+
for (; head; head = head->next) {
16+
nums.push_back(head->val);
17+
}
18+
stack<int> stk;
19+
int n = nums.size();
20+
vector<int> ans(n);
21+
for (int i = n - 1; ~i; --i) {
22+
while (!stk.empty() && stk.top() <= nums[i]) {
23+
stk.pop();
24+
}
25+
if (!stk.empty()) {
26+
ans[i] = stk.top();
27+
28+
}
29+
stk.push(nums[i]);
30+
}
31+
return ans;
32+
}
33+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func nextLargerNodes(head *ListNode) []int {
9+
nums := []int{}
10+
for ; head != nil; head = head.Next {
11+
nums = append(nums, head.Val)
12+
}
13+
stk := []int{}
14+
n := len(nums)
15+
ans := make([]int, n)
16+
for i := n - 1; i >= 0; i-- {
17+
for len(stk) > 0 && stk[len(stk)-1] <= nums[i] {
18+
stk = stk[:len(stk)-1]
19+
}
20+
if len(stk) > 0 {
21+
ans[i] = stk[len(stk)-1]
22+
}
23+
stk = append(stk, nums[i])
24+
}
25+
return ans
26+
}

0 commit comments

Comments
 (0)