JavaScript Simple Solution


  • 0
    I
    var addTwoNumbers = function (l1, l2) {
        let carry = 0, curNodeL1 = l1, curNodeL2 = l2, start = new ListNode(0), cur = start, tempSum;
        while (curNodeL1 || curNodeL2 || carry) {
            tempSum = (curNodeL1 ? curNodeL1.val : 0) + (curNodeL2 ? curNodeL2.val : 0) + carry;
            cur.next = new ListNode((tempSum) % 10);
            carry = Math.floor((tempSum) / 10);
            cur = cur.next;
            curNodeL1 = curNodeL1 ? curNodeL1.next : null;
            curNodeL2 = curNodeL2 ? curNodeL2.next : null;
        }
        return start.next;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.