-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_k_sorted_lists.rs
63 lines (56 loc) · 1.43 KB
/
merge_k_sorted_lists.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
///
/// Problem: Merge k Sorted Lists
///
/// You are given an array of `k` linked lists, each sorted in ascending order. Merge all the linked lists into one sorted linked list and return it.
///
/// Example 1:
/// Input: lists = [[1,4,5],[1,3,4],[2,6]]
/// Output: [1,1,2,3,4,4,5,6]
/// Explanation: The linked lists are:
/// [
/// 1 -> 4 -> 5,
/// 1 -> 3 -> 4,
/// 2 -> 6
/// ]
/// Merging them into one sorted list:
/// 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6
///
/// Example 2:
/// Input: lists = []
/// Output: []
///
/// Example 3:
/// Input: lists = [[]]
/// Output: []
///
/// # Solution:
///
/// Time complexity: O(N * log(k))
/// Space complexity: O(k)
use std::cmp::{Reverse, Ordering};
use std::collections::BinaryHeap;
type NodeOpt = Option<Box<ListNode>>;
impl Solution {
pub fn merge_k_lists(lists: Vec<NodeOpt>) -> NodeOpt {
let mut head = None;
let mut tail = &mut head;
let mut heap = BinaryHeap::from(lists);
while let Some(Some(mut curr)) = heap.pop() {
if curr.next.is_some() {
heap.push(curr.next.take());
}
tail = &mut tail.insert(curr).next;
}
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 {
Reverse(self.val).cmp(&Reverse(other.val))
}
}