MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / add_two_numbers_recursive

Method add_two_numbers_recursive

2. Add Two Numbers/src/main.rs:95–110  ·  view source on GitHub ↗
(l1: Option<&Box<ListNode>>, l2: Option<&Box<ListNode>>, carry: i32)

Source from the content-addressed store, hash-verified

93
94impl 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)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected