Ny leetcode
This commit is contained in:
parent
ec598dfada
commit
4f3385850f
2 changed files with 45 additions and 0 deletions
6
merge-two-sorted-lists/Cargo.toml
Normal file
6
merge-two-sorted-lists/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "merge-two-sorted-lists"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
39
merge-two-sorted-lists/src/main.rs
Normal file
39
merge-two-sorted-lists/src/main.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn merge_two_lists(
|
||||||
|
list1: Option<Box<ListNode>>,
|
||||||
|
list2: Option<Box<ListNode>>,
|
||||||
|
) -> Option<Box<ListNode>> {
|
||||||
|
match (list1, list2) {
|
||||||
|
(Some(mut node1), Some(mut node2)) => {
|
||||||
|
if node1.val < node2.val {
|
||||||
|
let next = node1.next.take(); // Take ownership of `node1.next`
|
||||||
|
node1.next = merge_two_lists(next, Some(node2));
|
||||||
|
Some(node1)
|
||||||
|
} else {
|
||||||
|
let next = node2.next.take(); // Take ownership of `node2.next`
|
||||||
|
node2.next = merge_two_lists(Some(node1), next);
|
||||||
|
Some(node2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Some(node1), None) => Some(node1),
|
||||||
|
(None, Some(node2)) => Some(node2),
|
||||||
|
(None, None) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue