forked from ben1009/leetcode-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp0002_add_two_numbers.rs
126 lines (114 loc) · 3.33 KB
/
p0002_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* [2] Add Two Numbers
*
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
*
* <strong class="example">Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;" />
* Input: l1 = [2,4,3], l2 = [5,6,4]
* Output: [7,0,8]
* Explanation: 342 + 465 = 807.
*
* <strong class="example">Example 2:
*
* Input: l1 = [0], l2 = [0]
* Output: [0]
*
* <strong class="example">Example 3:
*
* Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
* Output: [8,9,9,9,0,0,0,1]
*
*
* Constraints:
*
* The number of nodes in each linked list is in the range [1, 100].
* 0 <= Node.val <= 9
* It is guaranteed that the list represents a number that does not have leading zeros.
*
*/
pub struct Solution {}
use crate::util::linked_list::{to_list, ListNode};
// problem: https://leetcode.com/problems/add-two-numbers/
// discuss: https://leetcode.com/problems/add-two-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
// 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>> {
if l1.is_none() {
return l2;
}
if l2.is_none() {
return l1;
}
let (mut l1, mut l2) = (l1, l2);
let mut ret = Some(Box::new(ListNode::new(-1)));
let mut head = &mut ret;
let mut carry = 0;
loop {
if l1.is_none() && l2.is_none() && carry == 0 {
break;
}
let v1 = match l1 {
Some(n1) => {
l1 = n1.next;
n1.val
}
None => 0,
};
let v2 = match l2 {
Some(n2) => {
l2 = n2.next;
n2.val
}
None => 0,
};
let mut num = v1 + v2 + carry;
carry = num / 10;
num %= 10;
let next = ListNode::new(num);
head.as_mut().unwrap().next = Some(Box::new(next));
head = &mut head.as_mut().unwrap().next;
}
ret.unwrap().next
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2() {
assert_eq!(
Solution::add_two_numbers(to_list(vec![2, 4, 3]), to_list(vec![5, 6, 4])),
to_list(vec![7, 0, 8])
);
assert_eq!(
Solution::add_two_numbers(to_list(vec![9, 9, 9, 9]), to_list(vec![9, 9, 9, 9, 9, 9])),
to_list(vec![8, 9, 9, 9, 0, 0, 1])
);
assert_eq!(
Solution::add_two_numbers(to_list(vec![0]), to_list(vec![0])),
to_list(vec![0])
)
}
}