Skip to content

Commit fadafbb

Browse files
author
Zhang Xiaodong
committed
solve 23
1 parent 7219b88 commit fadafbb

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ mod s0019_remove_nth_node_from_end_of_list;
1818
mod s0020_valid_parentheses;
1919
mod s0021_merge_two_sorted_lists;
2020
mod s0022_generate_parentheses;
21+
mod s0023_merge_k_sorted_lists;
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* [23] 合并 K 个升序链表
3+
*
4+
* 给你一个链表数组,每个链表都已经按升序排列。
5+
* 请你将所有链表合并到一个升序链表中,返回合并后的链表。
6+
*
7+
* 示例 1:
8+
* 输入:lists = [[1,4,5],[1,3,4],[2,6]]
9+
* 输出:[1,1,2,3,4,4,5,6]
10+
* 解释:链表数组如下:
11+
* [
12+
* 1->4->5,
13+
* 1->3->4,
14+
* 2->6
15+
* ]
16+
* 将它们合并到一个有序链表中得到。
17+
* 1->1->2->3->4->4->5->6
18+
*
19+
* 示例 2:
20+
* 输入:lists = []
21+
* 输出:[]
22+
*
23+
* 示例 3:
24+
* 输入:lists = [[]]
25+
* 输出:[]
26+
*
27+
*
28+
* 提示:
29+
*
30+
* k == lists.length
31+
* 0 <= k <= 10^4
32+
* 0 <= lists[i].length <= 500
33+
* -10^4 <= lists[i][j] <= 10^4
34+
* lists[i] 按 升序 排列
35+
* lists[i].length 的总和不超过 10^4
36+
*
37+
*/
38+
pub struct Solution {}
39+
40+
use crate::util::linked_list::{to_list, ListNode};
41+
42+
// problem: https://leetcode.cn/problems/merge-k-sorted-lists/
43+
// discuss: https://leetcode.cn/problems/merge-k-sorted-lists/discuss/?currentPage=1&orderBy=most_votes&query=
44+
45+
// submission codes start here
46+
47+
use std::collections::BinaryHeap;
48+
49+
impl PartialOrd for Box<ListNode> {
50+
fn partial_cmp(&self, other: &Box<ListNode>) -> Option<std::cmp::Ordering> {
51+
if self.val < other.val {
52+
Some(std::cmp::Ordering::Greater)
53+
} else if self.val == other.val {
54+
Some(std::cmp::Ordering::Equal)
55+
} else {
56+
Some(std::cmp::Ordering::Less)
57+
}
58+
}
59+
}
60+
61+
impl Ord for Box<ListNode> {
62+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
63+
if self.val < other.val {
64+
std::cmp::Ordering::Greater
65+
} else if self.val == other.val {
66+
std::cmp::Ordering::Equal
67+
} else {
68+
std::cmp::Ordering::Less
69+
}
70+
}
71+
}
72+
73+
// Definition for singly-linked list.
74+
// #[derive(PartialEq, Eq, Clone, Debug)]
75+
// pub struct ListNode {
76+
// pub val: i32,
77+
// pub next: Option<Box<ListNode>>
78+
// }
79+
//
80+
// impl ListNode {
81+
// #[inline]
82+
// fn new(val: i32) -> Self {
83+
// ListNode {
84+
// next: None,
85+
// val
86+
// }
87+
// }
88+
// }
89+
impl Solution {
90+
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
91+
let mut heap = BinaryHeap::new();
92+
for p in lists {
93+
if let Some(e) = p {
94+
heap.push(e)
95+
}
96+
}
97+
let mut dummy_head = Some(Box::new(ListNode { next: None, val: 0 }));
98+
let mut curr = &mut dummy_head;
99+
loop {
100+
let mut p = heap.pop();
101+
if p.is_none() {
102+
break;
103+
}
104+
let mut p = p.unwrap();
105+
106+
let next = p.next.take();
107+
if let Some(next) = next {
108+
heap.push(next);
109+
}
110+
curr.as_mut().unwrap().next = Some(p);
111+
curr = &mut curr.as_mut().unwrap().next;
112+
}
113+
dummy_head.unwrap().next
114+
}
115+
}
116+
117+
// submission codes end
118+
119+
#[cfg(test)]
120+
mod tests {
121+
use super::*;
122+
123+
#[test]
124+
fn test_23() {
125+
assert_eq!(
126+
Solution::merge_k_lists(vec![
127+
to_list(vec![1, 4, 5]),
128+
to_list(vec![1, 3, 4]),
129+
to_list(vec![2, 6]),
130+
],),
131+
to_list(vec![1, 1, 2, 3, 4, 4, 5, 6])
132+
);
133+
assert_eq!(Solution::merge_k_lists(vec![]), None);
134+
assert_eq!(Solution::merge_k_lists(vec![None]), None);
135+
}
136+
}

0 commit comments

Comments
 (0)