-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmerge-k-sorted-lists.rs
88 lines (75 loc) · 2.35 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
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
#![allow(dead_code, unused, unused_variables)]
fn main() {}
struct Solution;
// 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 }
}
}
impl Solution {
pub fn merge_k_lists1(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
let mut lists = lists;
let (mut index, mut min_value) = (0usize, None);
for (i, v) in lists.iter().enumerate() {
match v {
Some(x) => {
if min_value.is_none() {
min_value = Some(x.val);
index = i;
} else {
if min_value.as_ref().unwrap().gt(&x.val) {
min_value = Some(x.val);
index = i;
}
}
}
None => (),
}
}
if index == 0 && min_value.is_none() {
return None;
}
let mut root = lists[index].take();
lists[index] = root.as_mut().unwrap().next.take();
root.as_mut().unwrap().next = Self::merge_k_lists(lists);
root
}
pub fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
let mut lists = lists;
if lists.is_empty() {
return None;
} else if lists.len() == 1 {
return lists[0].take();
}
let mut start = lists[0].take();
let mut i = 1usize;
while i < lists.len() {
start = Self::f(vec![start, lists[i].take()]);
i += 1;
}
start
}
fn f(mut lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
match (lists[0].take(), lists[1].take()) {
(Some(mut x), Some(mut y)) => {
if x.val < y.val {
x.next = Self::f(vec![x.next.take(), Some(y)]);
Some(x)
} else {
y.next = Self::f(vec![Some(x), y.next.take()]);
Some(y)
}
}
(Some(x), None) => Some(x),
(None, Some(y)) => Some(y),
(None, None) => None,
}
}
}