-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.rs
37 lines (37 loc) · 1014 Bytes
/
Solution.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
// 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(list), None) => Some(list),
(None, Some(list)) => Some(list),
(Some(mut list1), Some(mut list2)) => {
if list1.val < list2.val {
list1.next = Self::merge_two_lists(list1.next, Some(list2));
Some(list1)
} else {
list2.next = Self::merge_two_lists(Some(list1), list2.next);
Some(list2)
}
}
}
}
}