Is my code wrong? TLE error


  • 0
    J

    Is my code wrong?
    I can't pass the judgement,
    it said that I had a TLE error.
    what should I do ?

    var addTwoNumbers = function(l1, l2) {
    
        var l3 = new ListNode(0);
        return add(l1, l2, l3);
         
    };
    
    function add (nl1, nl2, nl3){
        var total = (nl1.val + nl2.val + nl3.val);
        nl3.val = total%10; 
        nl3.next = new ListNode(Math.floor(total/10));
        console.log(nl3);
        if(nl1.next && nl2.next){
            nl3.next = add(nl1.next, nl2.next, nl3.next);
            return nl3;
        }
        else if(nl1.next){
            nl3.next = add(nl1.next, new ListNode(0), nl3.next);
            return nl3;
        }
        else if(nl2.next){
            nl3.next = add(new ListNode(0), nl2.next, nl3.next);
            return nl3;
        }
        else{
            if(nl3.next.val === 0)nl3.next = null;
            return nl3;
        }
    }
    

Log in to reply
 

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