File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
S0024-swap-nodes-in-pairs/src Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 1
1
// Definition for singly-linked list.
2
2
use leetcode_prelude:: * ;
3
3
4
+ struct Solution ;
5
+ use std:: mem:: { replace, swap} ;
6
+
7
+ impl Solution {
8
+ pub fn swap_pairs ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
9
+ let mut head = head;
10
+ let mut cursor = & mut head;
11
+ while cursor. is_some ( ) && cursor. as_ref ( ) . unwrap ( ) . next . is_some ( ) {
12
+ let mut n2 = replace ( & mut cursor. as_mut ( ) . unwrap ( ) . next , None ) ;
13
+ swap (
14
+ & mut cursor. as_mut ( ) . unwrap ( ) . next ,
15
+ & mut n2. as_mut ( ) . unwrap ( ) . next ,
16
+ ) ;
17
+ swap ( & mut n2. as_mut ( ) . unwrap ( ) . next , cursor) ;
18
+ swap ( cursor, & mut n2) ;
19
+ cursor = & mut cursor. as_mut ( ) . unwrap ( ) . next . as_mut ( ) . unwrap ( ) . next ;
20
+ }
21
+
22
+ head
23
+ }
24
+ }
25
+ /*
4
26
struct Solution;
5
27
6
28
impl Solution {
@@ -27,6 +49,7 @@ impl Solution {
27
49
head
28
50
}
29
51
}
52
+ */
30
53
31
54
fn main ( ) {
32
55
let l = linkedlist ! [ 1 , 2 , 3 , 4 ] ;
You can’t perform that action at this time.
0 commit comments