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
| #[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 add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { let mut result = None; let mut tail = &mut result; let mut t = (l1,l2,0,0); loop { t = match t { (None,None,_,0) => break, (None,None,_,carry) => (None,None,carry,0), (Some(list),None,_,carry) | (None,Some(list),_,carry) if list.val + carry >= 10 => { (list.next,None,list.val+carry-10,1) } (Some(list),None,_,carry) | (None,Some(list),_,carry) => { (list.next,None,list.val+carry,0) } (Some(l1),Some(l2),_,carry) if l1.val + l2.val + carry >=10 => { (l1.next,l2.next,l1.val+l2.val+carry-10,1) } (Some(l1),Some(l2),_,carry) => { (l1.next,l2.next,l1.val+l2.val+carry,0) } }; *tail = Some(Box::new(ListNode::new(t.2))); tail = &mut tail.as_mut().unwrap().next; } result } }
|