feat: 23_merge_k_sorted_lists
This commit is contained in:
parent
c225277c72
commit
7a5e2cc77c
7
23_merge_k_sorted_lists/Cargo.lock
generated
Normal file
7
23_merge_k_sorted_lists/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "merge_k_sorted_lists"
|
||||
version = "0.1.0"
|
6
23_merge_k_sorted_lists/Cargo.toml
Normal file
6
23_merge_k_sorted_lists/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "merge_k_sorted_lists"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
62
23_merge_k_sorted_lists/src/lib.rs
Normal file
62
23_merge_k_sorted_lists/src/lib.rs
Normal file
@ -0,0 +1,62 @@
|
||||
#[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 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq)]
|
||||
struct List {
|
||||
head: Box<ListNode>,
|
||||
}
|
||||
|
||||
impl PartialEq for List {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.head.val == other.head.val
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for List {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
other.head.val.cmp(&self.head.val)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for List {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
|
||||
let mut result_head = None;
|
||||
let mut result = &mut result_head;
|
||||
|
||||
let mut heap = std::collections::BinaryHeap::<List>::new();
|
||||
for l in lists {
|
||||
if let Some(head) = l {
|
||||
heap.push(List { head: head });
|
||||
}
|
||||
}
|
||||
|
||||
while let Some(top) = heap.pop() {
|
||||
*result = Some(Box::new(ListNode::new(top.head.val)));
|
||||
result = &mut result.as_mut().unwrap().next;
|
||||
|
||||
if let Some(new_top_head) = top.head.next {
|
||||
heap.push(List { head: new_top_head });
|
||||
}
|
||||
}
|
||||
|
||||
result_head
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user