No Javascript solution possible


  • 0
    K

    Ideally, I think I have the correct(-ish) solution but in Javascript, because we are dealing with numbers too large in the test suite, there is no possible solution (or at least, the solution would be much more than medium complexity)

    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} l1
     * @param {ListNode} l2
     * @return {ListNode}
     */
    
    var toNum = function(listNode) {
        var power = 1;
        var num = 0;
        
        while(true) {
            num += listNode.val * power;
            power = power * 10;
            
            if (power < 100000) {
                console.log(num);
            }
            
            if (listNode.next)
                listNode = listNode.next;
            else return num;
        }
    }
    
    var toListNode = function(sum) {
        var num = sum % 10;
        var nums = [num];
        sum = Math.floor(sum/10)
        
        while (sum > 0) {
            var num = sum % 10;
            nums.push(num);
            sum = Math.floor(sum/10);
        }
        
        // nums = nums.reverse();
        
        return nums;
    }
    
    var addTwoNumbers = function(l1, l2) {
        num1 = toNum(l1);
        num2 = toNum(l2);
        
        console.log(num1 % 10000);
        console.log(num2 % 10000);
        console.log(num1 + num2)
        
        sum = num1 + num2;
        
        return toListNode(sum);
    };
    

Log in to reply
 

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