-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-two-sorted-lists.rs
37 lines (36 loc) · 1.11 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
https://leetcode.com/problems/merge-two-sorted-lists/
// 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_two_lists(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
match(list1, list2){
(None, None) => None,
(Some(left), None) => Some(left),
(None, Some(right)) => Some(right),
(Some(left), Some(right)) => match left.val <= right.val {
true => Some(Box::new(ListNode{
val: left.val,
next: Self::merge_two_lists(left.next, Some(right))
})),
false => Some(Box::new(ListNode{
val: right.val,
next: Self::merge_two_lists(Some(left), right.next)
}))
}
}
}
}