-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_two_sorted_lists.rs
58 lines (50 loc) · 1.67 KB
/
merge_two_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
use crate::list_node::ListNode;
use crate::list_node_additions::ListNodeAdditions;
/// You are given the heads of two sorted linked lists `list1` and `list2`.
///
/// Merge the two lists in a one sorted list. The list should be made by
/// splicing together the nodes of the first two lists.
///
/// Return the head of the merged linked list.
pub struct Solution;
impl Solution {
pub fn merge_two_lists(
list1: Option<Box<ListNode>>,
list2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
ListNodeAdditions::merge_lists(list1, list2)
}
}
#[cfg(test)]
mod tests {
use super::Solution;
use crate::list_node::ListNode;
use crate::list_node_additions::ListNodeAdditions;
#[test]
fn example_1() {
let items1 = vec![1, 2, 4];
let list1 = ListNodeAdditions::from_vec(items1);
let items2 = vec![1, 3, 4];
let list2 = ListNodeAdditions::from_vec(items2);
let result = Solution::merge_two_lists(list1, list2);
assert_eq!(result.to_vec(), vec![1, 1, 2, 3, 4, 4]);
}
#[test]
fn example_2() {
let items1 = vec![];
let list1 = ListNodeAdditions::from_vec(items1);
let items2 = vec![];
let list2 = ListNodeAdditions::from_vec(items2);
let result = Solution::merge_two_lists(list1, list2);
assert_eq!(result.to_vec(), vec![]);
}
#[test]
fn example_3() {
let items1 = vec![];
let list1 = ListNodeAdditions::from_vec(items1);
let items2 = vec![0];
let list2 = ListNodeAdditions::from_vec(items2);
let result = Solution::merge_two_lists(list1, list2);
assert_eq!(result.to_vec(), vec![0]);
}
}