44 * You are given two non-empty linked lists representing two non-negative
55 * integers. The digits are stored in reverse order and each of their nodes
66 * contain a single digit. Add the two numbers and return it as a linked list.
7- *
7+ *
88 * You may assume the two numbers do not contain any leading zero, except the
99 * number 0 itself.
10- *
10+ *
1111 * Example:
1212 *
1313 *
1717 *
1818 */
1919pub struct Solution { }
20- use super :: util:: linked_list:: { ListNode , to_list } ;
20+ use super :: util:: linked_list:: { to_list , ListNode } ;
2121
2222// submission codes start here
2323
2424impl 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 > > {
2629 let ( mut l1, mut l2) = ( l1, l2) ;
2730 let mut dummy_head = Some ( Box :: new ( ListNode :: new ( 0 ) ) ) ;
2831 let mut tail = & mut dummy_head;
2932 let ( mut l1_end, mut l2_end, mut overflow) = ( false , false , false ) ;
3033 loop {
3134 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+ }
3443 } ;
3544 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+ }
3853 } ;
3954 // if l1, l2 end and there is not overflow from previous operation, return the result
4055 if l1_end && l2_end && !overflow {
41- break dummy_head. unwrap ( ) . next
56+ break dummy_head. unwrap ( ) . next ;
4257 }
4358 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+ } ;
4566 tail. as_mut ( ) . unwrap ( ) . next = Some ( Box :: new ( ListNode :: new ( sum) ) ) ;
4667 tail = & mut tail. as_mut ( ) . unwrap ( ) . next
4768 }
@@ -50,17 +71,25 @@ impl Solution {
5071
5172// submission codes end
5273
53-
5474#[ cfg( test) ]
5575mod tests {
5676 use super :: * ;
5777
5878 #[ test]
5979 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+ ) ;
6184
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+ ) ;
6389
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+ )
6594 }
6695}
0 commit comments