|
| 1 | +// Given the head of a linked list, return the list after sorting it in ascending order. |
| 2 | + |
| 3 | +// Example 1: |
| 4 | + |
| 5 | +// Input: head = [4,2,1,3] |
| 6 | +// Output: [1,2,3,4] |
| 7 | +// Example 2: |
| 8 | + |
| 9 | +// Input: head = [-1,5,3,4,0] |
| 10 | +// Output: [-1,0,3,4,5] |
| 11 | +// Example 3: |
| 12 | + |
| 13 | +// Input: head = [] |
| 14 | +// Output: [] |
| 15 | + |
| 16 | +// Constraints: |
| 17 | + |
| 18 | +// The number of nodes in the list is in the range [0, 5 * 104]. |
| 19 | +// -105 <= Node.val <= 105 |
| 20 | + |
| 21 | +// Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)? |
| 22 | + |
| 23 | +use crate::util::linked_list::ListNode; |
| 24 | + |
| 25 | +// Definition for singly-linked list. |
| 26 | +// #[derive(PartialEq, Eq, Clone, Debug)] |
| 27 | +// pub struct ListNode { |
| 28 | +// pub val: i32, |
| 29 | +// pub next: Option<Box<ListNode>> |
| 30 | +// } |
| 31 | +// |
| 32 | +// impl ListNode { |
| 33 | +// #[inline] |
| 34 | +// fn new(val: i32) -> Self { |
| 35 | +// ListNode { |
| 36 | +// next: None, |
| 37 | +// val |
| 38 | +// } |
| 39 | +// } |
| 40 | +// } |
| 41 | +pub struct Solution; |
| 42 | + |
| 43 | +impl Solution { |
| 44 | + pub fn sort_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { |
| 45 | + if head.is_none() || head.as_ref().unwrap().next.is_none() { |
| 46 | + return head; |
| 47 | + } |
| 48 | + |
| 49 | + let (h1, h2) = Solution::half_cut(head); |
| 50 | + let h1 = Solution::sort_list(h1); |
| 51 | + let h2 = Solution::sort_list(h2); |
| 52 | + |
| 53 | + Solution::merge(h1, h2) |
| 54 | + } |
| 55 | + |
| 56 | + fn merge( |
| 57 | + mut h1: Option<Box<ListNode>>, |
| 58 | + mut h2: Option<Box<ListNode>>, |
| 59 | + ) -> Option<Box<ListNode>> { |
| 60 | + if h1.is_none() { |
| 61 | + return h2; |
| 62 | + } |
| 63 | + if h2.is_none() { |
| 64 | + return h1; |
| 65 | + } |
| 66 | + |
| 67 | + let mut head_pointer = ListNode::new(0); |
| 68 | + let mut pre = &mut head_pointer; |
| 69 | + while h1.is_some() || h2.is_some() { |
| 70 | + if h1.is_none() { |
| 71 | + pre.next = h2.clone(); |
| 72 | + break; |
| 73 | + } |
| 74 | + if h2.is_none() { |
| 75 | + pre.next = h1.clone(); |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + if h1.as_ref().unwrap().val < h2.as_ref().unwrap().val { |
| 80 | + pre.next = h1.clone(); |
| 81 | + h1 = h1.as_ref().unwrap().next.clone(); |
| 82 | + } else { |
| 83 | + pre.next = h2.clone(); |
| 84 | + h2 = h2.as_ref().unwrap().next.clone(); |
| 85 | + } |
| 86 | + pre = pre.next.as_deref_mut().unwrap(); |
| 87 | + } |
| 88 | + |
| 89 | + head_pointer.next |
| 90 | + } |
| 91 | + |
| 92 | + fn half_cut(head: Option<Box<ListNode>>) -> (Option<Box<ListNode>>, Option<Box<ListNode>>) { |
| 93 | + if head.is_none() || head.as_ref().unwrap().next.is_none() { |
| 94 | + return (head, None); |
| 95 | + } |
| 96 | + |
| 97 | + let mut head_pointer = ListNode::new(0); |
| 98 | + head_pointer.next = head.clone(); |
| 99 | + |
| 100 | + let mut slow = &mut head_pointer; |
| 101 | + let mut fast = Some(Box::new(slow.clone())); |
| 102 | + while fast.is_some() && fast.as_ref().unwrap().next.is_some() { |
| 103 | + slow = slow.next.as_mut().unwrap(); |
| 104 | + fast = fast.as_ref().unwrap().next.as_ref().unwrap().next.clone(); |
| 105 | + } |
| 106 | + let head2 = slow.next.clone(); |
| 107 | + slow.next = None; |
| 108 | + |
| 109 | + (head_pointer.next, head2) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use super::*; |
| 116 | + use crate::util::linked_list; |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_148() { |
| 120 | + assert_eq!( |
| 121 | + Solution::sort_list(linked_list::to_list(vec![4, 2, 1, 3])), |
| 122 | + linked_list::to_list(vec![1, 2, 3, 4]) |
| 123 | + ); |
| 124 | + assert_eq!( |
| 125 | + Solution::sort_list(linked_list::to_list(vec![-1, 5, 3, 4, 0])), |
| 126 | + linked_list::to_list(vec![-1, 0, 3, 4, 5]) |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments