-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.rs
36 lines (36 loc) · 1.03 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
// 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 partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
let mut head = head;
let mut d1 = Some(Box::new(ListNode::new(0)));
let mut d2 = Some(Box::new(ListNode::new(0)));
let (mut t1, mut t2) = (&mut d1, &mut d2);
while let Some(mut node) = head {
head = node.next.take();
if node.val < x {
t1.as_mut().unwrap().next = Some(node);
t1 = &mut t1.as_mut().unwrap().next;
} else {
t2.as_mut().unwrap().next = Some(node);
t2 = &mut t2.as_mut().unwrap().next;
}
}
t1.as_mut().unwrap().next = d2.unwrap().next;
d1.unwrap().next
}
}