Recursive Javascript solution, beats 95%, 9 lines


  • 0
    M
    var addTwoNumbers = function(l1, l2, overflow) {
        overflow = overflow ? overflow : 0;
        if (l1 === null && l2 === null && !overflow)
            return null;
        let val = ((l1 ? l1.val : 0) + (l2 ? l2.val : 0) + overflow);
        let newNode = new ListNode(val%10);
        newNode.next = addTwoNumbers(l1 ? l1.next : null, l2 ? l2.next : null, Math.floor(val/10))
        return newNode;
    };
    

  • 0
    A

    @muxamilian it's not ture.


  • 0
    M

    What's not true? That it beats 95%? Yeah it's possible because it could have change since I posted it, because other people possibly posted faster solutions...


Log in to reply
 

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