File tree 3 files changed +97
-0
lines changed
solution/1000-1099/1019.Next Greater Node In Linked List
3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change @@ -110,6 +110,40 @@ class Solution {
110
110
}
111
111
```
112
112
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
+
113
147
### ** ...**
114
148
115
149
```
Original file line number Diff line number Diff line change @@ -124,6 +124,40 @@ class Solution {
124
124
}
125
125
```
126
126
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
+
127
161
### ** ...**
128
162
129
163
```
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments