File tree 3 files changed +175
-0
lines changed
solution/0000-0099/0025.Reverse Nodes in k-Group
3 files changed +175
-0
lines changed Original file line number Diff line number Diff line change @@ -377,6 +377,66 @@ public class Solution {
377
377
}
378
378
```
379
379
380
+ ### ** Rust**
381
+
382
+ ``` rust
383
+ // Definition for singly-linked list.
384
+ // #[derive(PartialEq, Eq, Clone, Debug)]
385
+ // pub struct ListNode {
386
+ // pub val: i32,
387
+ // pub next: Option<Box<ListNode>>
388
+ // }
389
+ //
390
+ // impl ListNode {
391
+ // #[inline]
392
+ // fn new(val: i32) -> Self {
393
+ // ListNode {
394
+ // next: None,
395
+ // val
396
+ // }
397
+ // }
398
+ // }
399
+ impl Solution {
400
+ pub fn reverse_k_group (head : Option <Box <ListNode >>, k : i32 ) -> Option <Box <ListNode >> {
401
+ fn reverse (head : Option <Box <ListNode >>) -> Option <Box <ListNode >> {
402
+ let mut head = head ;
403
+ let mut pre = None ;
404
+ while let Some (mut node ) = head {
405
+ head = node . next. take ();
406
+ node . next = pre . take ();
407
+ pre = Some (node );
408
+ }
409
+ pre
410
+ }
411
+
412
+ let mut dummy = Some (Box :: new (ListNode :: new (0 )));
413
+ let mut pre = & mut dummy ;
414
+ let mut cur = head ;
415
+ while cur . is_some () {
416
+ let mut q = & mut cur ;
417
+ for _ in 0 .. k - 1 {
418
+ if q . is_none () {
419
+ break ;
420
+ }
421
+ q = & mut q . as_mut (). unwrap (). next;
422
+ }
423
+ if q . is_none () {
424
+ pre . as_mut (). unwrap (). next = cur ;
425
+ return dummy . unwrap (). next;
426
+ }
427
+
428
+ let b = q . as_mut (). unwrap (). next. take ();
429
+ pre . as_mut (). unwrap (). next = reverse (cur );
430
+ while pre . is_some () && pre . as_mut (). unwrap (). next. is_some () {
431
+ pre = & mut pre . as_mut (). unwrap (). next;
432
+ }
433
+ cur = b ;
434
+ }
435
+ dummy . unwrap (). next
436
+ }
437
+ }
438
+ ```
439
+
380
440
### ** ...**
381
441
382
442
```
Original file line number Diff line number Diff line change @@ -362,6 +362,66 @@ public class Solution {
362
362
}
363
363
```
364
364
365
+ ### ** Rust**
366
+
367
+ ``` rust
368
+ // Definition for singly-linked list.
369
+ // #[derive(PartialEq, Eq, Clone, Debug)]
370
+ // pub struct ListNode {
371
+ // pub val: i32,
372
+ // pub next: Option<Box<ListNode>>
373
+ // }
374
+ //
375
+ // impl ListNode {
376
+ // #[inline]
377
+ // fn new(val: i32) -> Self {
378
+ // ListNode {
379
+ // next: None,
380
+ // val
381
+ // }
382
+ // }
383
+ // }
384
+ impl Solution {
385
+ pub fn reverse_k_group (head : Option <Box <ListNode >>, k : i32 ) -> Option <Box <ListNode >> {
386
+ fn reverse (head : Option <Box <ListNode >>) -> Option <Box <ListNode >> {
387
+ let mut head = head ;
388
+ let mut pre = None ;
389
+ while let Some (mut node ) = head {
390
+ head = node . next. take ();
391
+ node . next = pre . take ();
392
+ pre = Some (node );
393
+ }
394
+ pre
395
+ }
396
+
397
+ let mut dummy = Some (Box :: new (ListNode :: new (0 )));
398
+ let mut pre = & mut dummy ;
399
+ let mut cur = head ;
400
+ while cur . is_some () {
401
+ let mut q = & mut cur ;
402
+ for _ in 0 .. k - 1 {
403
+ if q . is_none () {
404
+ break ;
405
+ }
406
+ q = & mut q . as_mut (). unwrap (). next;
407
+ }
408
+ if q . is_none () {
409
+ pre . as_mut (). unwrap (). next = cur ;
410
+ return dummy . unwrap (). next;
411
+ }
412
+
413
+ let b = q . as_mut (). unwrap (). next. take ();
414
+ pre . as_mut (). unwrap (). next = reverse (cur );
415
+ while pre . is_some () && pre . as_mut (). unwrap (). next. is_some () {
416
+ pre = & mut pre . as_mut (). unwrap (). next;
417
+ }
418
+ cur = b ;
419
+ }
420
+ dummy . unwrap (). next
421
+ }
422
+ }
423
+ ```
424
+
365
425
### ** ...**
366
426
367
427
```
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
+ impl Solution {
18
+ pub fn reverse_k_group ( head : Option < Box < ListNode > > , k : i32 ) -> Option < Box < ListNode > > {
19
+ fn reverse ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
20
+ let mut head = head;
21
+ let mut pre = None ;
22
+ while let Some ( mut node) = head {
23
+ head = node. next . take ( ) ;
24
+ node. next = pre. take ( ) ;
25
+ pre = Some ( node) ;
26
+ }
27
+ pre
28
+ }
29
+
30
+ let mut dummy = Some ( Box :: new ( ListNode :: new ( 0 ) ) ) ;
31
+ let mut pre = & mut dummy;
32
+ let mut cur = head;
33
+ while cur. is_some ( ) {
34
+ let mut q = & mut cur;
35
+ for _ in 0 ..k - 1 {
36
+ if q. is_none ( ) {
37
+ break ;
38
+ }
39
+ q = & mut q. as_mut ( ) . unwrap ( ) . next ;
40
+ }
41
+ if q. is_none ( ) {
42
+ pre. as_mut ( ) . unwrap ( ) . next = cur;
43
+ return dummy. unwrap ( ) . next ;
44
+ }
45
+
46
+ let b = q. as_mut ( ) . unwrap ( ) . next . take ( ) ;
47
+ pre. as_mut ( ) . unwrap ( ) . next = reverse ( cur) ;
48
+ while pre. is_some ( ) && pre. as_mut ( ) . unwrap ( ) . next . is_some ( ) {
49
+ pre = & mut pre. as_mut ( ) . unwrap ( ) . next ;
50
+ }
51
+ cur = b;
52
+ }
53
+ dummy. unwrap ( ) . next
54
+ }
55
+ }
You can’t perform that action at this time.
0 commit comments