-
Notifications
You must be signed in to change notification settings - Fork 2
/
7WHec2.rs
77 lines (62 loc) · 2.16 KB
/
7WHec2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
// 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(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut length = 0;
let mut ptr = head.as_ref();
while ptr.is_some() {
length += 1;
ptr = ptr.and_then(|x| x.next.as_ref());
}
Self::sort(head, length)
}
pub fn sort(mut head: Option<Box<ListNode>>, length: i32) -> Option<Box<ListNode>> {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return head;
}
let mut middle = head.as_mut();
for _j in 0..length / 2 - 1 {
middle = middle.and_then(|x| x.next.as_mut());
}
let right = middle.and_then(|x| x.next.take());
let mut right = Self::sort(right, length - (length / 2));
let mut left = Self::sort(head, length / 2);
let mut ans = Some(Box::new(ListNode::new(-1)));
let mut current = &mut ans.as_mut().unwrap().next;
while left.is_some() || right.is_some() {
if left.is_none() {
current.insert(right.unwrap());
break;
} else if right.is_none() {
current.insert(left.unwrap());
break;
} else {
if left.as_ref().unwrap().val < right.as_ref().unwrap().val {
let new_left = left.as_mut().unwrap().next.take();
current.insert(left.unwrap());
left = new_left;
} else {
let new_right = right.as_mut().unwrap().next.take();
current.insert(right.unwrap());
right = new_right;
}
current = &mut current.as_mut().unwrap().next;
}
}
ans.and_then(|mut x| x.next.take())
}
}