Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a9a53b1

Browse files
committed
src/bin/swap-nodes-in-pairs.rs
1 parent b7d4b0a commit a9a53b1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/bin/swap-nodes-in-pairs.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
fn main() {}
2+
3+
struct Solution;
4+
5+
// Definition for singly-linked list.
6+
#[derive(PartialEq, Eq, Clone, Debug)]
7+
pub struct ListNode {
8+
pub val: i32,
9+
pub next: Option<Box<ListNode>>,
10+
}
11+
12+
impl ListNode {
13+
#[inline]
14+
fn new(val: i32) -> Self {
15+
ListNode {
16+
next: None,
17+
val,
18+
}
19+
}
20+
}
21+
22+
impl Solution {
23+
pub fn swap_pairs(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
24+
match head {
25+
Some(mut x) => {
26+
match x.next.take() {
27+
Some(mut y) => {
28+
x.next = Self::swap_pairs(y.next.take());
29+
y.next = Some(x);
30+
Some(y)
31+
}
32+
None => Some(x)
33+
}
34+
}
35+
None => None
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)