|
| 1 | +/** |
| 2 | + * [0002] Add Two Numbers |
| 3 | + * |
| 4 | + * 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. |
| 5 | + * You may assume the two numbers do not contain any leading zero, except the number 0 itself. |
| 6 | + * |
| 7 | + * Example 1: |
| 8 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;" /> |
| 9 | + * Input: l1 = [2,4,3], l2 = [5,6,4] |
| 10 | + * Output: [7,0,8] |
| 11 | + * Explanation: 342 + 465 = 807. |
| 12 | + * |
| 13 | + * Example 2: |
| 14 | + * |
| 15 | + * Input: l1 = [0], l2 = [0] |
| 16 | + * Output: [0] |
| 17 | + * |
| 18 | + * Example 3: |
| 19 | + * |
| 20 | + * Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] |
| 21 | + * Output: [8,9,9,9,0,0,0,1] |
| 22 | + * |
| 23 | + * |
| 24 | + * Constraints: |
| 25 | + * |
| 26 | + * The number of nodes in each linked list is in the range [1, 100]. |
| 27 | + * 0 <= Node.val <= 9 |
| 28 | + * It is guaranteed that the list represents a number that does not have leading zeros. |
| 29 | + * |
| 30 | + */ |
| 31 | +pub struct Solution {} |
| 32 | +use crate::util::linked_list::{ListNode, to_list}; |
| 33 | + |
| 34 | +// problem: https://leetcode.com/problems/add-two-numbers/ |
| 35 | +// discuss: https://leetcode.com/problems/add-two-numbers/discuss/?currentPage=1&orderBy=most_votes&query= |
| 36 | + |
| 37 | +// submission codes start here |
| 38 | + |
| 39 | +// Definition for singly-linked list. |
| 40 | +// #[derive(PartialEq, Eq, Clone, Debug)] |
| 41 | +// pub struct ListNode { |
| 42 | +// pub val: i32, |
| 43 | +// pub next: Option<Box<ListNode>> |
| 44 | +// } |
| 45 | +// |
| 46 | +// impl ListNode { |
| 47 | +// #[inline] |
| 48 | +// fn new(val: i32) -> Self { |
| 49 | +// ListNode { |
| 50 | +// next: None, |
| 51 | +// val |
| 52 | +// } |
| 53 | +// } |
| 54 | +// } |
| 55 | +impl Solution { |
| 56 | + pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { |
| 57 | + let (mut l1, mut l2) = (l1, l2); |
| 58 | + let mut dummy_head = Some(Box::new(ListNode::new(0))); |
| 59 | + let mut cur = &dummy_head; |
| 60 | + let (mut l1_end, mut l2_end, mut overflow) = (false, false, false); |
| 61 | + loop { |
| 62 | + let v1 = match l1 { |
| 63 | + None => { |
| 64 | + l1_end = true; |
| 65 | + 0 |
| 66 | + } |
| 67 | + Some(node) => { |
| 68 | + l1 = node.next; |
| 69 | + node.val |
| 70 | + } |
| 71 | + }; |
| 72 | + let v2 = match l2 { |
| 73 | + None => { |
| 74 | + l2_end = true; |
| 75 | + 0 |
| 76 | + } |
| 77 | + Some(node) => { |
| 78 | + l2 = node.next; |
| 79 | + node.val |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + if (l1_end && l2_end && !overflow) { |
| 84 | + return dummy_head.unwrap().next; |
| 85 | + } |
| 86 | + let sum = v1 + v2 + if overflow {1} else {0}; |
| 87 | + let sum = if sum >= 0 { |
| 88 | + overflow = true; |
| 89 | + sum - 10 |
| 90 | + } else { |
| 91 | + overflow = false; |
| 92 | + sum |
| 93 | + }; |
| 94 | + |
| 95 | + cur.a |
| 96 | + } |
| 97 | + Some(Box::new(ListNode::new(0))) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// submission codes end |
| 102 | + |
| 103 | +#[cfg(test)] |
| 104 | +mod tests { |
| 105 | + use super::*; |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_0002() { |
| 109 | + } |
| 110 | +} |
0 commit comments