Skip to content

Commit e5ed98a

Browse files
committed
feat: add solutions to lc problem: No.1019
No.1019.Next Greater Node In Linked List
1 parent cfc4025 commit e5ed98a

File tree

4 files changed

+123
-64
lines changed

4 files changed

+123
-64
lines changed

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

+50-19
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@
4848

4949
**方法一:单调栈**
5050

51-
我们先遍历链表,将链表中的值存入数组 `nums`
51+
题目要求找到链表中每个节点的下一个更大的节点,即找到链表中每个节点的右边第一个比它大的节点。我们先遍历链表,将链表中的值存入数组 $nums$ 中。那么对于数组 $nums$ 中的每个元素,我们只需要找到它右边第一个比它大的元素即可。求下一个更大的元素的问题可以使用单调栈来解决
5252

53-
然后从后往前遍历数组 `nums`,维护一个从栈底到栈顶单调递减的栈 `stk`,遍历过程中,如果栈顶元素小于等于当前元素,则将栈顶元素出栈,直到栈顶元素大于当前元素或者栈为空。
53+
我们从后往前遍历数组 $nums$,维护一个从栈底到栈顶单调递减的栈 $stk$,遍历过程中,如果栈顶元素小于等于当前元素,则循环将栈顶元素出栈,直到栈顶元素大于当前元素或者栈为空。
5454

55-
如果栈为空,则说明当前元素没有下一个更大的元素,否则当前元素的下一个更大的元素就是栈顶元素,更新答案数组 `ans`
55+
如果此时栈为空,则说明当前元素没有下一个更大的元素,否则当前元素的下一个更大的元素就是栈顶元素,更新答案数组 $ans$。然后将当前元素入栈,继续遍历
5656

57-
然后将当前元素入栈,继续遍历。
58-
59-
遍历结束后,返回答案数组 `ans` 即可。
57+
遍历结束后,返回答案数组 $ans$ 即可。
6058

6159
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
6260

@@ -202,37 +200,70 @@ func nextLargerNodes(head *ListNode) []int {
202200
```js
203201
/**
204202
* Definition for singly-linked list.
205-
* function ListNode(val) {
206-
* this.val = val;
207-
* this.next = null;
203+
* function ListNode(val, next) {
204+
* this.val = (val===undefined ? 0 : val)
205+
* this.next = (next===undefined ? null : next)
208206
* }
209207
*/
210208
/**
211209
* @param {ListNode} head
212210
* @return {number[]}
213211
*/
214212
var nextLargerNodes = function (head) {
215-
let nums = [];
216-
while (head != null) {
213+
const nums = [];
214+
while (head) {
217215
nums.push(head.val);
218216
head = head.next;
219217
}
218+
const stk = [];
220219
const n = nums.length;
221-
let larger = new Array(n).fill(0);
222-
let stack = [];
223-
for (let i = 0; i < n; i++) {
224-
let num = nums[i];
225-
while (stack.length > 0 && nums[stack[stack.length - 1]] < num) {
226-
larger[stack.pop()] = num;
220+
const ans = new Array(n).fill(0);
221+
for (let i = n - 1; i >= 0; --i) {
222+
while (stk.length && stk[stk.length - 1] <= nums[i]) {
223+
stk.pop();
227224
}
228-
stack.push(i);
225+
ans[i] = stk.length ? stk[stk.length - 1] : 0;
226+
stk.push(nums[i]);
229227
}
230-
return larger;
228+
return ans;
231229
};
232230
```
233231

234232
### **TypeScript**
235233

234+
```ts
235+
/**
236+
* Definition for singly-linked list.
237+
* class ListNode {
238+
* val: number
239+
* next: ListNode | null
240+
* constructor(val?: number, next?: ListNode | null) {
241+
* this.val = (val===undefined ? 0 : val)
242+
* this.next = (next===undefined ? null : next)
243+
* }
244+
* }
245+
*/
246+
247+
function nextLargerNodes(head: ListNode | null): number[] {
248+
const nums: number[] = [];
249+
while (head) {
250+
nums.push(head.val);
251+
head = head.next;
252+
}
253+
const stk: number[] = [];
254+
const n = nums.length;
255+
const ans: number[] = new Array(n).fill(0);
256+
for (let i = n - 1; ~i; --i) {
257+
while (stk.length && stk[stk.length - 1] <= nums[i]) {
258+
stk.pop();
259+
}
260+
ans[i] = stk.length ? stk[stk.length - 1] : 0;
261+
stk.push(nums[i]);
262+
}
263+
return ans;
264+
}
265+
```
266+
236267
```ts
237268
/**
238269
* Definition for singly-linked list.

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

+46-13
Original file line numberDiff line numberDiff line change
@@ -174,37 +174,70 @@ func nextLargerNodes(head *ListNode) []int {
174174
```js
175175
/**
176176
* Definition for singly-linked list.
177-
* function ListNode(val) {
178-
* this.val = val;
179-
* this.next = null;
177+
* function ListNode(val, next) {
178+
* this.val = (val===undefined ? 0 : val)
179+
* this.next = (next===undefined ? null : next)
180180
* }
181181
*/
182182
/**
183183
* @param {ListNode} head
184184
* @return {number[]}
185185
*/
186186
var nextLargerNodes = function (head) {
187-
let nums = [];
188-
while (head != null) {
187+
const nums = [];
188+
while (head) {
189189
nums.push(head.val);
190190
head = head.next;
191191
}
192+
const stk = [];
192193
const n = nums.length;
193-
let larger = new Array(n).fill(0);
194-
let stack = [];
195-
for (let i = 0; i < n; i++) {
196-
let num = nums[i];
197-
while (stack.length > 0 && nums[stack[stack.length - 1]] < num) {
198-
larger[stack.pop()] = num;
194+
const ans = new Array(n).fill(0);
195+
for (let i = n - 1; i >= 0; --i) {
196+
while (stk.length && stk[stk.length - 1] <= nums[i]) {
197+
stk.pop();
199198
}
200-
stack.push(i);
199+
ans[i] = stk.length ? stk[stk.length - 1] : 0;
200+
stk.push(nums[i]);
201201
}
202-
return larger;
202+
return ans;
203203
};
204204
```
205205

206206
### **TypeScript**
207207

208+
```ts
209+
/**
210+
* Definition for singly-linked list.
211+
* class ListNode {
212+
* val: number
213+
* next: ListNode | null
214+
* constructor(val?: number, next?: ListNode | null) {
215+
* this.val = (val===undefined ? 0 : val)
216+
* this.next = (next===undefined ? null : next)
217+
* }
218+
* }
219+
*/
220+
221+
function nextLargerNodes(head: ListNode | null): number[] {
222+
const nums: number[] = [];
223+
while (head) {
224+
nums.push(head.val);
225+
head = head.next;
226+
}
227+
const stk: number[] = [];
228+
const n = nums.length;
229+
const ans: number[] = new Array(n).fill(0);
230+
for (let i = n - 1; ~i; --i) {
231+
while (stk.length && stk[stk.length - 1] <= nums[i]) {
232+
stk.pop();
233+
}
234+
ans[i] = stk.length ? stk[stk.length - 1] : 0;
235+
stk.push(nums[i]);
236+
}
237+
return ans;
238+
}
239+
```
240+
208241
```ts
209242
/**
210243
* Definition for singly-linked list.
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/**
22
* Definition for singly-linked list.
3-
* function ListNode(val) {
4-
* this.val = val;
5-
* this.next = null;
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
66
* }
77
*/
88
/**
99
* @param {ListNode} head
1010
* @return {number[]}
1111
*/
1212
var nextLargerNodes = function (head) {
13-
let nums = [];
14-
while (head != null) {
13+
const nums = [];
14+
while (head) {
1515
nums.push(head.val);
1616
head = head.next;
1717
}
18+
const stk = [];
1819
const n = nums.length;
19-
let larger = new Array(n).fill(0);
20-
let stack = [];
21-
for (let i = 0; i < n; i++) {
22-
let num = nums[i];
23-
while (stack.length > 0 && nums[stack[stack.length - 1]] < num) {
24-
larger[stack.pop()] = num;
20+
const ans = new Array(n).fill(0);
21+
for (let i = n - 1; i >= 0; --i) {
22+
while (stk.length && stk[stk.length - 1] <= nums[i]) {
23+
stk.pop();
2524
}
26-
stack.push(i);
25+
ans[i] = stk.length ? stk[stk.length - 1] : 0;
26+
stk.push(nums[i]);
2727
}
28-
return larger;
28+
return ans;
2929
};

solution/1000-1099/1019.Next Greater Node In Linked List/Solution.ts

+14-19
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,21 @@
1010
* }
1111
*/
1212

13-
interface Item {
14-
index: number;
15-
val: number;
16-
}
17-
1813
function nextLargerNodes(head: ListNode | null): number[] {
19-
const res: number[] = [];
20-
const stack: Item[] = [];
21-
let cur = head;
22-
for (let i = 0; cur != null; i++) {
23-
res.push(0);
24-
const { val, next } = cur;
25-
while (stack.length !== 0 && stack[stack.length - 1].val < val) {
26-
res[stack.pop().index] = val;
14+
const nums: number[] = [];
15+
while (head) {
16+
nums.push(head.val);
17+
head = head.next;
18+
}
19+
const stk: number[] = [];
20+
const n = nums.length;
21+
const ans: number[] = new Array(n).fill(0);
22+
for (let i = n - 1; ~i; --i) {
23+
while (stk.length && stk[stk.length - 1] <= nums[i]) {
24+
stk.pop();
2725
}
28-
stack.push({
29-
val,
30-
index: i,
31-
});
32-
cur = next;
26+
ans[i] = stk.length ? stk[stk.length - 1] : 0;
27+
stk.push(nums[i]);
3328
}
34-
return res;
29+
return ans;
3530
}

0 commit comments

Comments
 (0)