-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0025-reverse-nodes-in-k-group.rs
48 lines (41 loc) · 1.34 KB
/
0025-reverse-nodes-in-k-group.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
impl Solution {
pub fn reverse_k_group(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
let mut dummy = Some(Box::new(ListNode {next: head, val: 0 }));
let mut cur = dummy.as_mut();
'outer: loop {
let mut start = cur.as_mut().unwrap().next.take();
if start.is_none() {
break 'outer;
}
let mut end = start.as_mut();
for _ in 0..(k - 1) {
end = end.unwrap().next.as_mut();
if end.is_none() {
cur.as_mut().unwrap().next = start;
break 'outer;
}
}
let mut tail = end.as_mut().unwrap().next.take();
let end = Solution::reverse(start, tail);
cur.as_mut().unwrap().next = end;
for _ in 0..k {
cur = cur.unwrap().next.as_mut()
}
}
dummy.unwrap().next
}
fn reverse(
mut head: Option<Box<ListNode>>,
tail: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut prev = tail;
let mut cur = head;
while let Some(mut cur_node) = cur {
let mut next = cur_node.next.take();
cur_node.next = prev.take();
prev = Some(cur_node);
cur = next
}
prev
}
}