|
| 1 | +/* |
| 2 | + * [2] Add Two Numbers |
| 3 | + * |
| 4 | + * https://leetcode.com/problems/add-two-numbers/description/ |
| 5 | + * |
| 6 | + * algorithms |
| 7 | + * Medium (30.03%) |
| 8 | + * Total Accepted: 705.4K |
| 9 | + * Total Submissions: 2.3M |
| 10 | + * Testcase Example: '[2,4,3]\n[5,6,4]' |
| 11 | + * |
| 12 | + * You are given two non-empty linked lists representing two non-negative |
| 13 | + * integers. The digits are stored in reverse order and each of their nodes |
| 14 | + * contain a single digit. Add the two numbers and return it as a linked list. |
| 15 | + * |
| 16 | + * You may assume the two numbers do not contain any leading zero, except the |
| 17 | + * number 0 itself. |
| 18 | + * |
| 19 | + * Example: |
| 20 | + * |
| 21 | + * |
| 22 | + * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) |
| 23 | + * Output: 7 -> 0 -> 8 |
| 24 | + * Explanation: 342 + 465 = 807. |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +#[derive(PartialEq, Eq, Debug)] |
| 29 | +pub struct ListNode { |
| 30 | + pub val: i32, |
| 31 | + pub next: Option<Box<ListNode>> |
| 32 | +} |
| 33 | + |
| 34 | +impl ListNode { |
| 35 | + #[inline] |
| 36 | + fn new(val: i32) -> Self { |
| 37 | + ListNode { |
| 38 | + next: None, |
| 39 | + val |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// helper function for test |
| 45 | +pub fn to_list(vec: Vec<i32>) -> Option<Box<ListNode>> { |
| 46 | + let mut current = None; |
| 47 | + for &v in vec.iter().rev() { |
| 48 | + let mut node = ListNode::new(v); |
| 49 | + node.next = current; |
| 50 | + current = Some(Box::new(node)); |
| 51 | + } |
| 52 | + current |
| 53 | +} |
| 54 | + |
| 55 | +pub struct Solution {} |
| 56 | + |
| 57 | +// submission codes start here |
| 58 | + |
| 59 | +impl Solution { |
| 60 | + pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { |
| 61 | + let (mut l1, mut l2) = (l1, l2); |
| 62 | + let mut curr = None; |
| 63 | + let (mut l1_end, mut l2_end, mut overflow) = (false, false, false); |
| 64 | + loop { |
| 65 | + let lhs = match l1 { |
| 66 | + Some(node) => { l1 = node.next; node.val }, |
| 67 | + None => { l1_end = true; 0 }, |
| 68 | + }; |
| 69 | + let rhs = match l2 { |
| 70 | + Some(node) => { l2 = node.next; node.val }, |
| 71 | + None => { l2_end = true; 0 } |
| 72 | + }; |
| 73 | + // if l1, l2 end and there is not overflow from previous operation, return the result |
| 74 | + if l1_end && l2_end && !overflow { |
| 75 | + break Solution::reverse(curr); |
| 76 | + } |
| 77 | + let sum = lhs + rhs + if overflow { 1 } else { 0 }; |
| 78 | + let sum = if sum >= 10 { overflow = true; sum - 10 } else { overflow = false; sum }; |
| 79 | + let mut node = ListNode::new(sum); |
| 80 | + node.next = curr; |
| 81 | + curr = Some(Box::new(node)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + fn reverse(l: Option<Box<ListNode>>) -> Option<Box<ListNode>> { |
| 86 | + let mut prev = None; |
| 87 | + let mut current = l; |
| 88 | + while let Some(mut current_node_inner) = current { |
| 89 | + let next = current_node_inner.next; |
| 90 | + current_node_inner.next = prev; |
| 91 | + prev = Some(current_node_inner); |
| 92 | + current = next; |
| 93 | + } |
| 94 | + prev |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// submission codes end |
| 99 | + |
| 100 | +#[cfg(test)] |
| 101 | +mod tests { |
| 102 | + use super::*; |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_2() { |
| 106 | + assert_eq!(Solution::add_two_numbers(to_list(vec![2, 4, 3]), to_list(vec![5, 6, 4])), to_list(vec![7, 0, 8])); |
| 107 | + |
| 108 | + 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])); |
| 109 | + } |
| 110 | +} |
0 commit comments