-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
178 lines (166 loc) · 5.29 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
fn main() {
Solution::merge_k_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
}
))
}
)),
]
);
}
// Definition for singly-linked list.
#[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
}
}
}
struct Solution {}
use std::collections::BinaryHeap;
use std::cmp::Ordering;
/// implement the PartialOrd and Ord trait reversely to make a min heap
impl PartialOrd<ListNode> for ListNode {
fn partial_cmp(&self, other: &ListNode) -> Option<Ordering> {
other.val.partial_cmp(&self.val)
}
}
impl Ord for ListNode {
fn cmp(&self, other: &Self) -> Ordering {
other.val.cmp(&self.val)
}
}
impl Solution {
/// this is not written by myself XD, refresh
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
let mut heap: BinaryHeap<Box<ListNode>> = BinaryHeap::new();
for mut node in lists { // move inside
if node.is_some() {
heap.push(node.take()?);
// ? can be used when returning -> Option<Box<ListNode>>
}
}
// now all useful boxes' ownership are all in the heap
let mut head = heap.pop()?; // min
let mut ptr = &mut head;
while !heap.is_empty() {
if ptr.next.is_some() {
heap.push(ptr.next.take()?);
}
ptr.next = Some(heap.pop()?);
ptr = ptr.next.as_mut()?;
}
Some(head)
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
let n1 = ListNode::new(6);
let n2 = ListNode { val: 5, next: Some(Box::new(n1)) };
let n3 = ListNode { val: 4, next: Some(Box::new(n2)) };
let n4 = ListNode { val: 4, next: Some(Box::new(n3)) };
let n5 = ListNode { val: 3, next: Some(Box::new(n4)) };
let n6 = ListNode { val: 2, next: Some(Box::new(n5)) };
let n7 = ListNode { val: 1, next: Some(Box::new(n6)) };
let n8 = ListNode { val: 1, next: Some(Box::new(n7)) };
assert_eq!(
Some(Box::new(n8)),
Solution::merge_k_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
}
))
}
)),
]
)
)
}
}