Medium
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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Constraints:
[1, 100]
.0 <= Node.val <= 9
// Definition for singly-linked list.
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn add_two_numbers(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>
) -> Option<Box<ListNode>> {
let mut p = l1;
let mut q = l2;
let mut dummy_head = Box::new(ListNode::new(0));
let mut curr = &mut dummy_head;
let mut carry = 0;
while p.is_some() || q.is_some() {
let x = p.as_ref().map_or(0, |node| node.val);
let y = q.as_ref().map_or(0, |node| node.val);
let sum = carry + x + y;
carry = sum / 10;
curr.next = Some(Box::new(ListNode::new(sum % 10)));
curr = curr.next.as_mut().unwrap();
if let Some(node) = p {
p = node.next;
}
if let Some(node) = q {
q = node.next;
}
}
if carry > 0 {
curr.next = Some(Box::new(ListNode::new(carry)));
}
dummy_head.next
}
}