Skip to content

Commit 9636401

Browse files
authored
Create 1030-next-greater-node-in-linked-list.js
1 parent 649c688 commit 9636401

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
const nextLargerNodes = function(head) {
13+
const A = []
14+
while (head != null) A.push(head.val), (head = head.next)
15+
const res = new Array(A.length).fill(0)
16+
const stack = []
17+
for (let i = 0; i < A.length; i++) {
18+
while (stack.length && A[stack[stack.length - 1]] < A[i])
19+
res[stack.pop()] = A[i]
20+
stack.push(i)
21+
}
22+
return res
23+
}

0 commit comments

Comments
 (0)