Javascript n(n+m) clear


  • 1
    H
    var addTwoNumbers = function(l1, l2) {
        var digit = -1, val, up = 0, list = new ListNode(digit), result = list;
        
        while(l1 || l2 || up !==0) {
            digit = (l1 !== null ? l1.val : 0) + (l2 !== null ? l2.val : 0) + up;
            
            if (digit >= 10) {
                val = digit % 10;
                up = 1;
            } else {
                val = digit;
                up = 0;
            }
            if (l1 !== null) {
                l1 = l1.next;
            }
            if (l2 !== null) {
                l2 = l2.next;
            }
    
            list.next = new ListNode(val);
            list = list.next;
    
        }
        return result.next;
        
    };

  • 0
    D

    clear and useful! but i think maybe the val is redundant? just digit=digit % 10 might be ok.


Log in to reply
 

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