h0sny
h0sny

Reputation: 11

Adding value 0 to linked list node , Every other value works only number 0 doesnt work

I have two linked lists and I am creating a third list were every node is the sum of the nodes of list1 and list2 of the same index. It works fine, however when the sum of two nodes is greater than 9, say it's 15, then I subtract 15 - 10 = 5. This will become my current sum. I add this value to the next sum, say if the next sum is 4 then it will be 4+5 = 9.

The issue is when the sum = 10, then it will become 0. But when it's zero it refuses to create and append a new node to the list.

Here is my code:

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */

function add(val){ //returns new node
    console.log(val);
    let node = new ListNode() ;
    node = new ListNode(val , null)
    if(!head){
        head = tail = node;   
        head.next = null;
        tail.next = null;
    } 
    else{    
        tail.next = node;
        node.next = null;
    }
    return node;
}

let head , tail;

var addTwoNumbers = function(l1, l2) {
    let answer = "";
    let sum ;
    let node = null;
    let isLarge = false;
    while(l1 || l2){ //looping while any node exists
        sum = 0; //initializing sum to 0
        if(l1 && !l2)sum += l1.val; 
        else if(l2 && !l1) sum += l2.val;
        else sum += l1.val + l2.val;   
        if(isLarge){ //if prev sum was > 10
            sum++;
            isLarge = false;
        }
        if(sum > 9){ //if sum > 10
            sum = sum - 10;
            isLarge = true;
        }  
        add(sum);      
        l1 = l1.next;
        l2 = l2.next;
    }
    return head;
}; 

Upvotes: 0

Views: 1363

Answers (1)

trincot
trincot

Reputation: 350310

There are the following issues:

  • The add function does not update the tail reference, except when the list was empty. But this should happen always: the tail should reference the added node.

  • The main loop advances l1 and l2 even though one of them could be null. When that happens, an exception occurs. This update should only be done for the references that are not null

  • Even when l1 and l2 have become null, there might still be a carry, i.e. isLarge could be true after the loop ends. In that case you need to add an extra node to the result list, with value 1. You would do this after the loop.

  • You will get into problems when this code is run several times (for instance as part of a test suite): the global variables will retain the information from the previous call of addTwoNumbers, and so subsequent results will be wrong. You should not use global variables. Adapt your code such that head and tail are local variables. You would then also need to adapt the function add, since it can modify both head and tail. It will probably be easier to just integrate that add-logic inside addTwoNumbers.

When those two things are fixed, you'll get the correct result.

Here is the code where these issues are fixed, and some other things are done in a more compact way:

function ListNode(val, next) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
}

var addTwoNumbers = function(l1, l2) {
    let sum;
    let head = null;
    let tail = null;
    let carry = 0;
    while (l1 || l2) {
        sum = carry;
        if (l1) {
            sum += l1.val;
            l1 = l1.next;
        }
        if (l2) {
            sum += l2.val;
            l2 = l2.next;
        }
        carry = +(sum > 9);
        sum %= 10;
        let node = new ListNode(sum);
        if (head) {
            tail.next = node;
        } else {
            head = node;
        }
        tail = node;
    }
    if (carry) {
        tail.next = new ListNode(1);
    }
    return head;
};

// Some utility functions for this demo:
function from(...arr) {
    let head;
    for (let val of arr.reverse()) {
        head = new ListNode(val, head);
    }
    return head;
}

function * iter(lst) {
    while (lst) {
        yield lst.val;
        lst = lst.next;
    }
}

// Demo
let l1 = from(2, 0, 1, 9);
let l2 = from(8, 4, 3, 1);
let l3 = addTwoNumbers(l1, l2);
console.log(...iter(l3)); // 0 5 4 0 1

Upvotes: 2

Related Questions