200ms js solution


  • 0
    L
    var addTwoNumbers = function(l1, l2) {
        function add (l1, l2, carry = 0) {
            if (!l1 && !l2 && !carry) {
                return null;
            }
            if (!l1) {
                return add(l2, new ListNode(carry));
            }
            if (!l2) {
                return add(l1, new ListNode(carry));
            }
            const sum = l1.val + l2.val + carry;
            const result = new ListNode(sum % 10);
            result.next = add(l1.next, l2.next, parseInt(sum / 10, 10));
            return result;
        };
        return add(l1, l2);
    };
    

Log in to reply
 

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