Implemented p23 with 2 different solutions
This commit is contained in:
parent
e57953fd8c
commit
b7efae53d1
1 changed files with 83 additions and 20 deletions
|
@ -1,11 +1,24 @@
|
||||||
use super::linked_list::ListNode;
|
pub mod sorted_vec {
|
||||||
|
use crate::linked_list::ListNode;
|
||||||
|
|
||||||
pub struct Solution;
|
pub struct Solution;
|
||||||
impl Solution {
|
impl Solution {
|
||||||
// TODO: implement
|
|
||||||
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
|
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
|
||||||
|
let mut nums = vec![];
|
||||||
return None;
|
for mut list in lists {
|
||||||
|
while let Some(node) = list {
|
||||||
|
nums.push(node.val);
|
||||||
|
list = node.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nums.sort();
|
||||||
|
let mut head = None;
|
||||||
|
let mut tail = &mut head;
|
||||||
|
for num in nums {
|
||||||
|
*tail = Some(Box::new(ListNode::new(num)));
|
||||||
|
tail = &mut tail.as_mut().unwrap().next;
|
||||||
|
}
|
||||||
|
return head;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,3 +38,53 @@ fn test2() {
|
||||||
fn test3() {
|
fn test3() {
|
||||||
assert_eq!(Solution::merge_k_lists(vec![list![]]), list![]);
|
assert_eq!(Solution::merge_k_lists(vec![list![]]), list![]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod binary_heap {
|
||||||
|
use crate::linked_list::ListNode;
|
||||||
|
use std::{collections::BinaryHeap, cmp::{Ordering, Reverse}};
|
||||||
|
|
||||||
|
pub struct Solution;
|
||||||
|
impl Solution {
|
||||||
|
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
|
||||||
|
let mut lists = lists.into_iter()
|
||||||
|
.filter(Option::is_some)
|
||||||
|
.map(Reverse)
|
||||||
|
.collect::<BinaryHeap<_>>();
|
||||||
|
let mut head = None;
|
||||||
|
let mut tail = &mut head;
|
||||||
|
while let Some(Reverse(mut list)) = lists.pop() {
|
||||||
|
let next = list.as_mut().unwrap().next.take();
|
||||||
|
*tail = list;
|
||||||
|
tail = &mut tail.as_mut().unwrap().next;
|
||||||
|
if next.is_some() { lists.push(Reverse(next)); }
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for ListNode {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ord for ListNode {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering { self.val.cmp(&other.val) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test1() {
|
||||||
|
assert_eq!(Solution::merge_k_lists(vec![
|
||||||
|
list![1,4,5], list![1,3,4], list![2,6]
|
||||||
|
]), list![1,1,2,3,4,4,5,6]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test2() {
|
||||||
|
assert_eq!(Solution::merge_k_lists(vec![]), list![]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test3() {
|
||||||
|
assert_eq!(Solution::merge_k_lists(vec![list![]]), list![]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue