-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.rs
More file actions
83 lines (76 loc) · 1.83 KB
/
main.rs
File metadata and controls
83 lines (76 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn reverse_k_group(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
let mut head = head;
let (mut tail, ok) = Solution::forward_k_steps(&mut head, k);
if !ok {
head
} else {
head = Solution::reverse_list(head, None);
tail = Solution::reverse_k_group(tail, k);
Solution::connect(head, tail)
}
}
fn forward_k_steps(src: &mut Option<Box<ListNode>>, k: i32) -> (Option<Box<ListNode>>, bool) {
if let Some(src) = src {
if k == 1 {
(src.next.take(), true)
} else {
Solution::forward_k_steps(&mut src.next, k - 1)
}
} else {
(None, false)
}
}
fn reverse_list(
src: Option<Box<ListNode>>,
built: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
if let Some(mut src1) = src {
let tail = src1.next.take();
std::mem::replace(&mut src1.next, built);
Solution::reverse_list(tail, Some(src1))
} else {
built
}
}
fn connect(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if let Some(mut head) = list1 {
let tail = head.next.take();
std::mem::replace(&mut head.next, Solution::connect(tail, list2));
Some(head)
} else {
list2
}
}
}
struct Solution;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
fn main() {
println!("Hello, world!");
}