forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
56 lines (54 loc) · 1.71 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
56
// 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 sort_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
fn merge(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
match (l1, l2) {
(None, Some(node)) | (Some(node), None) => Some(node),
(Some(mut node1), Some(mut node2)) => {
if node1.val < node2.val {
node1.next = merge(node1.next.take(), Some(node2));
Some(node1)
} else {
node2.next = merge(Some(node1), node2.next.take());
Some(node2)
}
}
_ => None,
}
}
fn sort(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return head;
}
let mut head = head;
let mut length = 0;
let mut cur = &head;
while cur.is_some() {
length += 1;
cur = &cur.as_ref().unwrap().next;
}
let mut cur = &mut head;
for _ in 0..length / 2 - 1 {
cur = &mut cur.as_mut().unwrap().next;
}
let right = cur.as_mut().unwrap().next.take();
merge(sort(head), sort(right))
}
sort(head)
}
}