Javascript solution


  • 4
    C
    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} l1
     * @param {ListNode} l2
     * @return {ListNode}
     */
    var addTwoNumbers = function(l1, l2) {
        var List = new ListNode(0);
        var head = List;
        var sum = 0;
        var carry = 0;
    
        while(l1!==null||l2!==null||sum>0){
    
            if(l1!==null){
                sum = sum + l1.val;
                l1 = l1.next;
            }
            if(l2!==null){
                sum = sum + l2.val;
                l2 = l2.next;
            }
            if(sum>=10){
                carry = 1;
                sum = sum - 10;
            }
    
            head.next = new ListNode(sum);
            head = head.next;
    
            sum = carry;
            carry = 0;
    
        }
    
        return List.next;
    };

  • 0
    G

    I want to try again !thanks


  • 0
    T
    This post is deleted!

Log in to reply
 

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