-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmerge_k_lists.rs
86 lines (79 loc) · 2.68 KB
/
merge_k_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use super::ListNode;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
// 将链表头节点加入堆
let mut heap = BinaryHeap::new();
for node in lists.into_iter().flatten() {
heap.push(Reverse(node));
}
// 虚拟头结点
let mut dummy = Box::new(ListNode { val: 0, next: None });
// 指向虚拟头节点尾部节点,用于设置新节点
let mut tail = &mut dummy;
// 从堆中逐个取出节点,设置到新链表上,
// 并把取出节点的next节点重新加入堆中
while let Some(Reverse(mut node)) = heap.pop() {
if let Some(next) = node.next.take() {
heap.push(Reverse(next));
}
tail.next = Some(node);
tail = tail.next.as_mut().unwrap();
}
dummy.next
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_merge_k_lists() {
let ret1 = merge_k_lists(Vec::new());
assert!(ret1.is_none());
let lists = vec![
Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode {
val: 4,
next: Some(Box::new(ListNode { val: 5, next: None })),
})),
})),
Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode {
val: 3,
next: Some(Box::new(ListNode { val: 4, next: None })),
})),
})),
Some(Box::new(ListNode {
val: 2,
next: Some(Box::new(ListNode { val: 6, next: None })),
})),
];
let ret2 = merge_k_lists(lists);
assert_eq!(
ret2,
Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode {
val: 2,
next: Some(Box::new(ListNode {
val: 3,
next: Some(Box::new(ListNode {
val: 4,
next: Some(Box::new(ListNode {
val: 4,
next: Some(Box::new(ListNode {
val: 5,
next: Some(Box::new(ListNode { val: 6, next: None }))
}))
}))
}))
}))
}))
}))
}))
);
}
}