File tree 3 files changed +127
-0
lines changed
solution/0100-0199/0143.Reorder List
3 files changed +127
-0
lines changed Original file line number Diff line number Diff line change @@ -366,6 +366,50 @@ function reorderList(head: ListNode | null): void {
366
366
}
367
367
```
368
368
369
+ ### ** Rust**
370
+
371
+ ``` rust
372
+ // Definition for singly-linked list.
373
+ // #[derive(PartialEq, Eq, Clone, Debug)]
374
+ // pub struct ListNode {
375
+ // pub val: i32,
376
+ // pub next: Option<Box<ListNode>>
377
+ // }
378
+ //
379
+ // impl ListNode {
380
+ // #[inline]
381
+ // fn new(val: i32) -> Self {
382
+ // ListNode {
383
+ // next: None,
384
+ // val
385
+ // }
386
+ // }
387
+ // }
388
+ use std :: collections :: VecDeque ;
389
+ impl Solution {
390
+ pub fn reorder_list (head : & mut Option <Box <ListNode >>) {
391
+ let mut tail = & mut head . as_mut (). unwrap (). next;
392
+ let mut head = tail . take ();
393
+ let mut deque = VecDeque :: new ();
394
+ while head . is_some () {
395
+ let next = head . as_mut (). unwrap (). next. take ();
396
+ deque . push_back (head );
397
+ head = next ;
398
+ }
399
+ let mut flag = false ;
400
+ while ! deque . is_empty () {
401
+ * tail = if flag {
402
+ deque . pop_front (). unwrap ()
403
+ } else {
404
+ deque . pop_back (). unwrap ()
405
+ };
406
+ tail = & mut tail . as_mut (). unwrap (). next;
407
+ flag = ! flag ;
408
+ }
409
+ }
410
+ }
411
+ ```
412
+
369
413
### ** ...**
370
414
371
415
```
Original file line number Diff line number Diff line change @@ -349,6 +349,50 @@ function reorderList(head: ListNode | null): void {
349
349
}
350
350
```
351
351
352
+ ### ** Rust**
353
+
354
+ ``` rust
355
+ // Definition for singly-linked list.
356
+ // #[derive(PartialEq, Eq, Clone, Debug)]
357
+ // pub struct ListNode {
358
+ // pub val: i32,
359
+ // pub next: Option<Box<ListNode>>
360
+ // }
361
+ //
362
+ // impl ListNode {
363
+ // #[inline]
364
+ // fn new(val: i32) -> Self {
365
+ // ListNode {
366
+ // next: None,
367
+ // val
368
+ // }
369
+ // }
370
+ // }
371
+ use std :: collections :: VecDeque ;
372
+ impl Solution {
373
+ pub fn reorder_list (head : & mut Option <Box <ListNode >>) {
374
+ let mut tail = & mut head . as_mut (). unwrap (). next;
375
+ let mut head = tail . take ();
376
+ let mut deque = VecDeque :: new ();
377
+ while head . is_some () {
378
+ let next = head . as_mut (). unwrap (). next. take ();
379
+ deque . push_back (head );
380
+ head = next ;
381
+ }
382
+ let mut flag = false ;
383
+ while ! deque . is_empty () {
384
+ * tail = if flag {
385
+ deque . pop_front (). unwrap ()
386
+ } else {
387
+ deque . pop_back (). unwrap ()
388
+ };
389
+ tail = & mut tail . as_mut (). unwrap (). next;
390
+ flag = ! flag ;
391
+ }
392
+ }
393
+ }
394
+ ```
395
+
352
396
### ** ...**
353
397
354
398
```
Original file line number Diff line number Diff line change
1
+ // Definition for singly-linked list.
2
+ // #[derive(PartialEq, Eq, Clone, Debug)]
3
+ // pub struct ListNode {
4
+ // pub val: i32,
5
+ // pub next: Option<Box<ListNode>>
6
+ // }
7
+ //
8
+ // impl ListNode {
9
+ // #[inline]
10
+ // fn new(val: i32) -> Self {
11
+ // ListNode {
12
+ // next: None,
13
+ // val
14
+ // }
15
+ // }
16
+ // }
17
+ use std:: collections:: VecDeque ;
18
+ impl Solution {
19
+ pub fn reorder_list ( head : & mut Option < Box < ListNode > > ) {
20
+ let mut tail = & mut head. as_mut ( ) . unwrap ( ) . next ;
21
+ let mut head = tail. take ( ) ;
22
+ let mut deque = VecDeque :: new ( ) ;
23
+ while head. is_some ( ) {
24
+ let next = head. as_mut ( ) . unwrap ( ) . next . take ( ) ;
25
+ deque. push_back ( head) ;
26
+ head = next;
27
+ }
28
+ let mut flag = false ;
29
+ while !deque. is_empty ( ) {
30
+ * tail = if flag {
31
+ deque. pop_front ( ) . unwrap ( )
32
+ } else {
33
+ deque. pop_back ( ) . unwrap ( )
34
+ } ;
35
+ tail = & mut tail. as_mut ( ) . unwrap ( ) . next ;
36
+ flag = !flag;
37
+ }
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments