445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

Rust Solution

struct Solution;
use rustgym_util::*;

impl Solution {
    fn add_two_numbers(l1: ListLink, l2: ListLink) -> ListLink {
        let mut v1: Vec<i32> = vec![];
        let mut v2: Vec<i32> = vec![];
        let mut p1 = &l1;
        let mut p2 = &l2;
        while let Some(n1) = p1 {
            v1.push(n1.val);
            p1 = &n1.next;
        }
        while let Some(n2) = p2 {
            v2.push(n2.val);
            p2 = &n2.next;
        }
        let mut carry = 0;
        let mut res = None;
        while !v1.is_empty() || !v2.is_empty() || carry != 0 {
            let mut sum = 0;
            if let Some(x1) = v1.pop() {
                sum += x1;
            }
            if let Some(x2) = v2.pop() {
                sum += x2;
            }
            sum += carry;
            res = ListLink::link(sum % 10, res);
            carry = sum / 10;
        }
        res
    }
}

#[test]
fn test() {
    let l1 = list!(7, 2, 4, 3);
    let l2 = list!(5, 6, 4);
    let res = list!(7, 8, 0, 7);
    assert_eq!(Solution::add_two_numbers(l1, l2), res);
}

Having problems with this solution? Click here to submit an issue on github.