-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy path0021-merge-two-sorted-lists.rs
46 lines (45 loc) · 1.46 KB
/
0021-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
impl Solution {
pub fn merge_two_lists(
list1: Option<Box<ListNode>>,
list2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
match (list1, list2) {
(Some(list1), None) => Some(list1),
(None, Some(list2)) => Some(list2),
(None, None) => None,
(Some(l1), Some(l2)) => {
if l1.val < l2.val {
return Some(Box::new(ListNode {
val: l1.val,
next: Solution::merge_two_lists(l1.next, Some(l2)),
}));
} else {
return Some(Box::new(ListNode {
val: l2.val,
next: Solution::merge_two_lists(Some(l1), l2.next),
}));
}
}
}
}
}
//without recursion
impl Solution {
pub fn merge_two_lists(
mut l1: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
// Tail is the rightmost part of l1 that we keep swapping
let mut tail = &mut l1;
while l2.is_some() {
if tail.is_none() || l2.as_ref()?.val < tail.as_ref()?.val {
// Keep swapping the end part of l1
// so that the smallest nodes appear
// first in l1
std::mem::swap(tail, &mut l2);
}
tail = &mut tail.as_mut()?.next;
}
l1
}
}