20240524 Finished

This commit is contained in:
jackfiled 2024-05-24 09:25:16 +08:00
parent 852d92c5fe
commit c3d977118b
2 changed files with 69 additions and 1 deletions

View File

@ -137,4 +137,5 @@ mod p452_minimum_number_of_arrows_to_burst_balloons;
mod p71_simplify_path; mod p71_simplify_path;
mod p155_min_stack; mod p155_min_stack;
mod p150_evaluate_reverse_polish_notation; mod p150_evaluate_reverse_polish_notation;
mod p224_basic_calculator; mod p224_basic_calculator;
mod p21_merge_two_sorted_lists;

View File

@ -0,0 +1,67 @@
/**
* [21] Merge Two Sorted Lists
*/
pub struct Solution {}
use std::borrow::{Borrow, BorrowMut};
use crate::util::linked_list::{ListNode, to_list};
// submission codes start here
// 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>> {
let mut head = None;
let mut now = &mut head;
let (mut list1, mut list2) = (list1, list2);
*now = loop {
match (list1, list2) {
(Some(mut a), Some(mut b)) => {
if a.val < b.val {
list1 = a.next.take();
list2 = Some(b);
now = &mut now.insert(a).next;
} else {
list1 = Some(a);
list2 = b.next.take();
now = &mut now.insert(b).next
}
},
(x, y) => break x.or(y)
}
};
head
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_21() {
}
}