My use JavaScript! But is wrong,can your look my code!


  • 0
    G
    var addTwoNumbers = function(l1, l2) {
       var arr = [],
           carry = 0,
           len1 = l1.length,
           len2 = l2.length,
           len = Math.max(len1,len2);
        for (var i=0; i<len; i++){
            var sum = (l1[i]||0) + (l2[i]||0) + carry;  
            if (sum >= 10){
               carry = 1;
               sum -= 10;  
            } else {
                carry = 0; 
            }
            arr.push(sum);
            if (i == len-1 && carry == 1){
                arr.push(1);
            }
        }
        return arr;
    };

Log in to reply
 

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