@@ -203,12 +203,12 @@ function nextLargerNodes(head: ListNode | null): number[] {
203
203
}
204
204
const stk: number [] = [];
205
205
const n = nums .length ;
206
- const ans: number [] = new Array (n ).fill (0 );
206
+ const ans: number [] = Array (n ).fill (0 );
207
207
for (let i = n - 1 ; ~ i ; -- i ) {
208
- while (stk .length && stk [ stk . length - 1 ] <= nums [i ]) {
208
+ while (stk .length && stk . at ( - 1 ) ! <= nums [i ]) {
209
209
stk .pop ();
210
210
}
211
- ans [i ] = stk .length ? stk [ stk . length - 1 ] : 0 ;
211
+ ans [i ] = stk .length ? stk . at ( - 1 ) ! : 0 ;
212
212
stk .push (nums [i ]);
213
213
}
214
214
return ans ;
@@ -232,32 +232,29 @@ function nextLargerNodes(head: ListNode | null): number[] {
232
232
// }
233
233
// }
234
234
// }
235
- struct Item {
236
- index : usize ,
237
- val : i32 ,
238
- }
239
-
235
+ use std :: collections :: VecDeque ;
240
236
impl Solution {
241
237
pub fn next_larger_nodes (head : Option <Box <ListNode >>) -> Vec <i32 > {
242
- let mut res = Vec :: new ();
243
- let mut stack : Vec <Item > = Vec :: new ();
244
- let mut cur = & head ;
245
- for i in 0 .. usize :: MAX {
246
- if cur . is_none () {
247
- break ;
238
+ let mut nums = Vec :: new ();
239
+ let mut current = & head ;
240
+ while let Some (node ) = current {
241
+ nums . push (node . val);
242
+ current = & node . next;
243
+ }
244
+
245
+ let mut stk = VecDeque :: new ();
246
+ let n = nums . len ();
247
+ let mut ans = vec! [0 ; n ];
248
+ for i in (0 .. n ). rev () {
249
+ while ! stk . is_empty () && stk . back (). copied (). unwrap () <= nums [i ] {
250
+ stk . pop_back ();
248
251
}
249
- res . push (0 );
250
- let node = cur . as_ref (). unwrap ();
251
- while ! stack . is_empty () && stack . last (). unwrap (). val < node . val {
252
- res [stack . pop (). unwrap (). index] = node . val;
252
+ if let Some (& top ) = stk . back () {
253
+ ans [i ] = top ;
253
254
}
254
- stack . push (Item {
255
- index : i ,
256
- val : node . val,
257
- });
258
- cur = & node . next;
255
+ stk . push_back (nums [i ]);
259
256
}
260
- res
257
+ ans
261
258
}
262
259
}
263
260
```
@@ -296,48 +293,4 @@ var nextLargerNodes = function (head) {
296
293
297
294
<!-- tabs: end -->
298
295
299
- ### 方法二
300
-
301
- <!-- tabs: start -->
302
-
303
- ``` ts
304
- /**
305
- * Definition for singly-linked list.
306
- * class ListNode {
307
- * val: number
308
- * next: ListNode | null
309
- * constructor(val?: number, next?: ListNode | null) {
310
- * this.val = (val===undefined ? 0 : val)
311
- * this.next = (next===undefined ? null : next)
312
- * }
313
- * }
314
- */
315
-
316
- interface Item {
317
- index: number ;
318
- val: number ;
319
- }
320
-
321
- function nextLargerNodes(head : ListNode | null ): number [] {
322
- const res: number [] = [];
323
- const stack: Item [] = [];
324
- let cur = head ;
325
- for (let i = 0 ; cur != null ; i ++ ) {
326
- res .push (0 );
327
- const { val, next } = cur ;
328
- while (stack .length !== 0 && stack [stack .length - 1 ].val < val ) {
329
- res [stack .pop ().index ] = val ;
330
- }
331
- stack .push ({
332
- val ,
333
- index: i ,
334
- });
335
- cur = next ;
336
- }
337
- return res ;
338
- }
339
- ```
340
-
341
- <!-- tabs: end -->
342
-
343
296
<!-- end -->
0 commit comments