-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.add-two-numbers.rs
51 lines (45 loc) · 1.14 KB
/
2.add-two-numbers.rs
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
46
47
48
49
50
/*
* @lc app=leetcode id=2 lang=rust
*
* [2] Add Two Numbers
*/
// @lc code=start
// 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
// }
// }
// }
impl Solution {
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
Self::add(l1, l2, 0)
}
pub fn add(mut l1: Option<Box<ListNode>>, mut l2: Option<Box<ListNode>>, mut carry: i32) -> Option<Box<ListNode>> {
if (l1.is_none() && l2.is_none() && carry == 0) {
return None;
}
let mut sum = carry;
l1.take().map(|node| {
sum += node.val;
l1 = node.next;
});
l2.take().map(|node| {
sum += node.val;
l2 = node.next;
});
let mut node = ListNode::new(sum % 10);
node.next = Self::add(l1, l2, sum / 10);
Some(Box::new(node))
}
}
// @lc code=end