Skip to content

Commit 9e8335d

Browse files
committed
Add solution #1019
1 parent 3fef498 commit 9e8335d

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,093 LeetCode solutions in JavaScript
1+
# 1,094 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -827,6 +827,7 @@
827827
1016|[Binary String With Substrings Representing 1 To N](./solutions/1016-binary-string-with-substrings-representing-1-to-n.js)|Medium|
828828
1017|[Convert to Base -2](./solutions/1017-convert-to-base-2.js)|Medium|
829829
1018|[Binary Prefix Divisible By 5](./solutions/1018-binary-prefix-divisible-by-5.js)|Easy|
830+
1019|[Next Greater Node In Linked List](./solutions/1019-next-greater-node-in-linked-list.js)|Medium|
830831
1022|[Sum of Root To Leaf Binary Numbers](./solutions/1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
831832
1023|[Camelcase Matching](./solutions/1023-camelcase-matching.js)|Medium|
832833
1028|[Recover a Tree From Preorder Traversal](./solutions/1028-recover-a-tree-from-preorder-traversal.js)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1019. Next Greater Node In Linked List
3+
* https://leetcode.com/problems/next-greater-node-in-linked-list/
4+
* Difficulty: Medium
5+
*
6+
* You are given the head of a linked list with n nodes.
7+
*
8+
* For each node in the list, find the value of the next greater node. That is, for each node,
9+
* find the value of the first node that is next to it and has a strictly larger value than it.
10+
*
11+
* Return an integer array answer where answer[i] is the value of the next greater node of the
12+
* ith node (1-indexed). If the ith node does not have a next greater node, set answer[i] = 0.
13+
*/
14+
15+
/**
16+
* Definition for singly-linked list.
17+
* function ListNode(val, next) {
18+
* this.val = (val===undefined ? 0 : val)
19+
* this.next = (next===undefined ? null : next)
20+
* }
21+
*/
22+
/**
23+
* @param {ListNode} head
24+
* @return {number[]}
25+
*/
26+
var nextLargerNodes = function(head) {
27+
const values = [];
28+
let current = head;
29+
30+
while (current) {
31+
values.push(current.val);
32+
current = current.next;
33+
}
34+
35+
const result = new Array(values.length).fill(0);
36+
const stack = [];
37+
38+
for (let i = 0; i < values.length; i++) {
39+
while (stack.length && values[stack[stack.length - 1]] < values[i]) {
40+
result[stack.pop()] = values[i];
41+
}
42+
stack.push(i);
43+
}
44+
45+
return result;
46+
};

0 commit comments

Comments
 (0)