4
4
* You are given two non-empty linked lists representing two non-negative
5
5
* integers. The digits are stored in reverse order and each of their nodes
6
6
* contain a single digit. Add the two numbers and return it as a linked list.
7
- *
7
+ *
8
8
* You may assume the two numbers do not contain any leading zero, except the
9
9
* number 0 itself.
10
- *
10
+ *
11
11
* Example:
12
12
*
13
13
*
17
17
*
18
18
*/
19
19
pub struct Solution { }
20
- use super :: util:: linked_list:: { ListNode , to_list } ;
20
+ use super :: util:: linked_list:: { to_list , ListNode } ;
21
21
22
22
// submission codes start here
23
23
24
24
impl Solution {
25
- pub fn add_two_numbers ( l1 : Option < Box < ListNode > > , l2 : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
25
+ pub fn add_two_numbers (
26
+ l1 : Option < Box < ListNode > > ,
27
+ l2 : Option < Box < ListNode > > ,
28
+ ) -> Option < Box < ListNode > > {
26
29
let ( mut l1, mut l2) = ( l1, l2) ;
27
30
let mut dummy_head = Some ( Box :: new ( ListNode :: new ( 0 ) ) ) ;
28
31
let mut tail = & mut dummy_head;
29
32
let ( mut l1_end, mut l2_end, mut overflow) = ( false , false , false ) ;
30
33
loop {
31
34
let lhs = match l1 {
32
- Some ( node) => { l1 = node. next ; node. val } ,
33
- None => { l1_end = true ; 0 } ,
35
+ Some ( node) => {
36
+ l1 = node. next ;
37
+ node. val
38
+ }
39
+ None => {
40
+ l1_end = true ;
41
+ 0
42
+ }
34
43
} ;
35
44
let rhs = match l2 {
36
- Some ( node) => { l2 = node. next ; node. val } ,
37
- None => { l2_end = true ; 0 }
45
+ Some ( node) => {
46
+ l2 = node. next ;
47
+ node. val
48
+ }
49
+ None => {
50
+ l2_end = true ;
51
+ 0
52
+ }
38
53
} ;
39
54
// if l1, l2 end and there is not overflow from previous operation, return the result
40
55
if l1_end && l2_end && !overflow {
41
- break dummy_head. unwrap ( ) . next
56
+ break dummy_head. unwrap ( ) . next ;
42
57
}
43
58
let sum = lhs + rhs + if overflow { 1 } else { 0 } ;
44
- let sum = if sum >= 10 { overflow = true ; sum - 10 } else { overflow = false ; sum } ;
59
+ let sum = if sum >= 10 {
60
+ overflow = true ;
61
+ sum - 10
62
+ } else {
63
+ overflow = false ;
64
+ sum
65
+ } ;
45
66
tail. as_mut ( ) . unwrap ( ) . next = Some ( Box :: new ( ListNode :: new ( sum) ) ) ;
46
67
tail = & mut tail. as_mut ( ) . unwrap ( ) . next
47
68
}
@@ -50,17 +71,25 @@ impl Solution {
50
71
51
72
// submission codes end
52
73
53
-
54
74
#[ cfg( test) ]
55
75
mod tests {
56
76
use super :: * ;
57
77
58
78
#[ test]
59
79
fn test_2 ( ) {
60
- assert_eq ! ( Solution :: add_two_numbers( to_list( vec![ 2 , 4 , 3 ] ) , to_list( vec![ 5 , 6 , 4 ] ) ) , to_list( vec![ 7 , 0 , 8 ] ) ) ;
80
+ assert_eq ! (
81
+ Solution :: add_two_numbers( to_list( vec![ 2 , 4 , 3 ] ) , to_list( vec![ 5 , 6 , 4 ] ) ) ,
82
+ to_list( vec![ 7 , 0 , 8 ] )
83
+ ) ;
61
84
62
- 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 ] ) ) ;
85
+ assert_eq ! (
86
+ Solution :: add_two_numbers( to_list( vec![ 9 , 9 , 9 , 9 ] ) , to_list( vec![ 9 , 9 , 9 , 9 , 9 , 9 ] ) ) ,
87
+ to_list( vec![ 8 , 9 , 9 , 9 , 0 , 0 , 1 ] )
88
+ ) ;
63
89
64
- assert_eq ! ( Solution :: add_two_numbers( to_list( vec![ 0 ] ) , to_list( vec![ 0 ] ) ) , to_list( vec![ 0 ] ) )
90
+ assert_eq ! (
91
+ Solution :: add_two_numbers( to_list( vec![ 0 ] ) , to_list( vec![ 0 ] ) ) ,
92
+ to_list( vec![ 0 ] )
93
+ )
65
94
}
66
95
}
0 commit comments