-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.rs
55 lines (53 loc) · 1.59 KB
/
Solution.rs
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
// 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>> {
fn reverse(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut head = head;
let mut pre = None;
while let Some(mut node) = head {
head = node.next.take();
node.next = pre.take();
pre = Some(node);
}
pre
}
let mut dummy = Some(Box::new(ListNode::new(0)));
let mut pre = &mut dummy;
let mut cur = head;
while cur.is_some() {
let mut q = &mut cur;
for _ in 0..k - 1 {
if q.is_none() {
break;
}
q = &mut q.as_mut().unwrap().next;
}
if q.is_none() {
pre.as_mut().unwrap().next = cur;
return dummy.unwrap().next;
}
let b = q.as_mut().unwrap().next.take();
pre.as_mut().unwrap().next = reverse(cur);
while pre.is_some() && pre.as_mut().unwrap().next.is_some() {
pre = &mut pre.as_mut().unwrap().next;
}
cur = b;
}
dummy.unwrap().next
}
}