(l1: Option<&Box<ListNode>>, l2: Option<&Box<ListNode>>, carry: i32)
| 93 | |
| 94 | impl Solution { |
| 95 | fn add_two_numbers_recursive(l1: Option<&Box<ListNode>>, l2: Option<&Box<ListNode>>, carry: i32) -> Option<Box<ListNode>> { |
| 96 | match (l1, l2, carry) { |
| 97 | (Some(l1), Some(l2), carry) => { |
| 98 | let sum = l1.val + l2.val + carry; |
| 99 | Some(Box::new(ListNode { val: sum % 10, next: Self::add_two_numbers_recursive(l1.next.as_ref(), l2.next.as_ref(), sum / 10) })) |
| 100 | } |
| 101 | (Some(l), None, carry) | (None, Some(l), carry) => { |
| 102 | let sum = l.val + carry; |
| 103 | Some(Box::new(ListNode { val: sum % 10, next: Self::add_two_numbers_recursive(l.next.as_ref(), None, sum / 10) })) |
| 104 | } |
| 105 | (None, None, 1) => { |
| 106 | Some(Box::new(ListNode::new(1))) |
| 107 | } |
| 108 | _ => None |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { |
| 113 | Self::add_two_numbers_recursive(l1.as_ref(), l2.as_ref(), 0) |
nothing calls this directly
no outgoing calls
no test coverage detected