美文网首页
2-add-two-numbers

2-add-two-numbers

作者: 本一和他的朋友们 | 来源:发表于2019-03-31 11:51 被阅读0次

Add Two Numbers

You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand 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.
Example:

*Input:* (2 -> 4 -> 3) + (5 -> 6 -> 4)
*Output:* 7 -> 0 -> 8
*Explanation:* 342 + 465 = 807.

solution:

function listNode(val) {
    this.val = val;
    this.next = next;
}

function addTwoNumbers(l1, l2) {
    let sum = l1.val + l2.val;
    let next1 = l1.next;
    let next2 = l2.next;
    let l3 = new ListNode(sum % 10);
    let node = l3;

    sum = Math.floor(sum / 10);
    while (next1 || next2 || sum !== 0) {
        sum += (next1 ? next1.val : 0) + (next2 ? next2.val : 0);
        node.next = new listNode(sum % 10);
        node = node.next;
        next1 = next1 ? next1.next : null;
        next2 = next2 ? next2.next : null;
        sum = Math.floor(sum / 10);
    }

    return l3;
}

相关文章

网友评论

      本文标题:2-add-two-numbers

      本文链接:https://www.haomeiwen.com/subject/fmmybqtx.html