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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#![allow(dead_code)]
use crate::linked_list::*;
pub fn add_two_numbers2(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(0));
let mut cur = &mut dummy;
let mut sum = 0;
let mut l1 = l1.as_ref();
let mut l2 = l2.as_ref();
while l1.is_some() || l2.is_some() {
match l1 {
Some(ref node) => {
sum += node.val;
l1 = l1.unwrap().next.as_ref();
}
None => {}
}
match l2 {
Some(ref node) => {
sum += node.val;
l2 = l2.unwrap().next.as_ref();
}
None => {}
}
cur.next = Some(Box::new(ListNode::new(sum % 10)));
sum /= 10;
cur = cur.next.as_mut().unwrap();
}
if sum != 0 {
cur.next = Some(Box::new(ListNode::new(1)));
}
dummy.next
}
pub fn add_two_numbers(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let vec1 = list_to_vec(l1);
let vec2 = list_to_vec(l2);
let mut vec3 = vec![];
let mut carry = 0;
let min_len = vec1.len().min(vec2.len());
let max_len = vec1.len().max(vec2.len());
for i in 0..min_len {
let mut val = vec1[i] + vec2[i] + carry;
if val > 9 {
val -= 10;
vec3.push(val);
carry = 1;
} else {
vec3.push(val);
carry = 0;
}
}
let rest_vec = if max_len == vec1.len() { vec1 } else { vec2 };
for i in min_len..max_len {
let mut val = rest_vec[i] + carry;
if val > 9 {
val -= 10;
vec3.push(val);
carry = 1;
} else {
vec3.push(val);
carry = 0;
}
}
if carry == 1 {
vec3.push(1);
}
let last = vec3.len() - 1;
if vec3.len() > 1 && vec3[last] == 10 {
vec3[last] -= 10;
vec3.push(1);
}
vec_to_list(vec3)
}
fn list_to_vec(l: Option<Box<ListNode>>) -> Vec<i32> {
let mut nums = vec![];
let mut list = l;
while list.is_some() {
match list {
Some(ref node) => nums.push(node.val),
None => {}
};
list = list.unwrap().next;
}
nums
}
fn vec_to_list(arr: Vec<i32>) -> Option<Box<ListNode>> {
let mut head = Box::new(ListNode::new(0));
let mut cur = &mut head;
for num in arr.iter() {
cur.next = Some(Box::new(ListNode::new(*num)));
cur = cur.next.as_mut().unwrap();
}
head.next
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test1() {
let l1 = Some(Box::new(ListNode {
val: 2,
next: Some(Box::new(ListNode {
val: 4,
next: Some(Box::new(ListNode { val: 3, next: None })),
})),
}));
let l2 = Some(Box::new(ListNode {
val: 5,
next: Some(Box::new(ListNode {
val: 6,
next: Some(Box::new(ListNode { val: 4, next: None })),
})),
}));
let l3 = Some(Box::new(ListNode {
val: 7,
next: Some(Box::new(ListNode {
val: 0,
next: Some(Box::new(ListNode { val: 8, next: None })),
})),
}));
assert_eq!(add_two_numbers(l1, l2), l3);
}
#[test]
fn test2() {
let l1 = Some(Box::new(ListNode {
val: 2,
next: Some(Box::new(ListNode {
val: 4,
next: Some(Box::new(ListNode { val: 3, next: None })),
})),
}));
let l2 = Some(Box::new(ListNode {
val: 5,
next: Some(Box::new(ListNode {
val: 6,
next: Some(Box::new(ListNode { val: 4, next: None })),
})),
}));
let l3 = Some(Box::new(ListNode {
val: 7,
next: Some(Box::new(ListNode {
val: 0,
next: Some(Box::new(ListNode { val: 8, next: None })),
})),
}));
assert_eq!(add_two_numbers2(l1, l2), l3);
}
}