fn main() { println!("Hello, world!"); } #[derive(PartialEq, Eq, Clone, Debug)] pub struct ListNode { pub val: i32, pub next: Option> } impl ListNode { #[inline] fn new(val: i32) -> Self { ListNode { next: None, val } } } // Comprehensive 3ms Recursive struct Solution {} impl Default for ListNode { fn default() -> Self { ListNode::new(0) } } impl Solution { pub fn add_two_numbers(l1: Option>, l2: Option>) -> Option> { Solution::add_two_numbers_with_carry(l1, l2, 0) } pub fn add_two_numbers_with_carry(l1: Option>, l2: Option>, carry: i32) -> Option> { if l1.is_none() && l2.is_none() && carry == 0 {return None;} let a = l1.unwrap_or_default(); let b = l2.unwrap_or_default(); let sum = a.val + b.val + carry; let result = ListNode { val: sum % 10, next: Solution::add_two_numbers_with_carry(a.next, b.next, sum / 10) }; Some(Box::new(result)) } }