Skip to content

Commit f3267fc

Browse files
authored
feat: add javascript solution to lc problem: No.1019.Next Greater Node In Linked List (doocs#407)
1 parent 2b39851 commit f3267fc

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,40 @@ class Solution {
110110
}
111111
```
112112

113+
### **JavaScript**
114+
115+
```js
116+
/**
117+
* Definition for singly-linked list.
118+
* function ListNode(val) {
119+
* this.val = val;
120+
* this.next = null;
121+
* }
122+
*/
123+
/**
124+
* @param {ListNode} head
125+
* @return {number[]}
126+
*/
127+
var nextLargerNodes = function(head) {
128+
let nums = [];
129+
while (head != null) {
130+
nums.push(head.val);
131+
head = head.next;
132+
}
133+
const n = nums.length;
134+
let larger = new Array(n).fill(0);
135+
let stack = [];
136+
for (let i = 0; i < n; i++) {
137+
let num = nums[i];
138+
while (stack.length > 0 && nums[stack[stack.length - 1]] < num) {
139+
larger[stack.pop()] = num;
140+
}
141+
stack.push(i);
142+
}
143+
return larger;
144+
};
145+
```
146+
113147
### **...**
114148

115149
```

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

+34
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,40 @@ class Solution {
124124
}
125125
```
126126

127+
### **JavaScript**
128+
129+
```js
130+
/**
131+
* Definition for singly-linked list.
132+
* function ListNode(val) {
133+
* this.val = val;
134+
* this.next = null;
135+
* }
136+
*/
137+
/**
138+
* @param {ListNode} head
139+
* @return {number[]}
140+
*/
141+
var nextLargerNodes = function(head) {
142+
let nums = [];
143+
while (head != null) {
144+
nums.push(head.val);
145+
head = head.next;
146+
}
147+
const n = nums.length;
148+
let larger = new Array(n).fill(0);
149+
let stack = [];
150+
for (let i = 0; i < n; i++) {
151+
let num = nums[i];
152+
while (stack.length > 0 && nums[stack[stack.length - 1]] < num) {
153+
larger[stack.pop()] = num;
154+
}
155+
stack.push(i);
156+
}
157+
return larger;
158+
};
159+
```
160+
127161
### **...**
128162

129163
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {number[]}
11+
*/
12+
var nextLargerNodes = function(head) {
13+
let nums = [];
14+
while (head != null) {
15+
nums.push(head.val);
16+
head = head.next;
17+
}
18+
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;
25+
}
26+
stack.push(i);
27+
}
28+
return larger;
29+
};

0 commit comments

Comments
 (0)