-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0328_odd_even_linked_list.rs
More file actions
66 lines (61 loc) · 1.69 KB
/
s0328_odd_even_linked_list.rs
File metadata and controls
66 lines (61 loc) · 1.69 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
#![allow(unused)]
pub struct Solution {}
use crate::util::linked_list::{to_list, ListNode};
// 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 odd_even_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() {
return head;
}
let mut head1 = Some(Box::new(ListNode::new(-1)));
let mut head2 = Some(Box::new(ListNode::new(-1)));
let mut temp1 = &mut head1;
let mut temp2 = &mut head2;
let mut i = 1;
while let Some(x) = head {
if i % 2 == 1 {
temp1.as_mut().unwrap().next = Some(x);
temp1 = &mut temp1.as_mut().unwrap().next;
head = temp1.as_mut().unwrap().next.take();
} else {
temp2.as_mut().unwrap().next = Some(x);
temp2 = &mut temp2.as_mut().unwrap().next;
head = temp2.as_mut().unwrap().next.take();
}
i += 1;
}
temp1.as_mut().unwrap().next = head2.unwrap().next;
head1.unwrap().next
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_328() {
assert_eq!(
Solution::odd_even_list(to_list(vec![1, 2, 3, 4, 5])),
to_list(vec![1, 3, 5, 2, 4])
);
assert_eq!(
Solution::odd_even_list(to_list(vec![2, 1, 3, 5, 6, 4, 7])),
to_list(vec![2, 3, 6, 7, 1, 5, 4])
);
}
}