-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.rs
38 lines (38 loc) · 980 Bytes
/
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
// 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 l = ListNode::new(0);
let mut r = ListNode::new(0);
let mut tl = &mut l;
let mut tr = &mut r;
let mut current = head;
while let Some(mut node) = current {
current = node.next.take();
if node.val < x {
tl.next = Some(node);
tl = tl.next.as_mut().unwrap();
} else {
tr.next = Some(node);
tr = tr.next.as_mut().unwrap();
}
}
tr.next = None;
tl.next = r.next;
l.next
}
}