linked-listsingly-linked-list

Issue in carryovers while adding two numbers in a linked list


I'm solving the leetcode question for adding two numbers in a linked list.

The following is my code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* node1 = l1;
        ListNode* node2 = l2;

        if(!node1 && !node2){
            return l1;
        }
        if(!node1){
            return l2;
        }
        if(!node2){
            return l1;
        }
        while(node1 && node2){
                node1->val = node1->val + node2->val;
                node1 = node1->next;
                node2 = node2->next;    
        }
        if(!node1 && !node2){
            return l1;
        }
        if(!node2){
            node1 = node1->next;
        }
        if(!node1){
            node1 = node2;
        }
        node1 = l1;
        if(!node1){
            return l1;
        }
        while(l1->next != nullptr){
            if(l1->val >9){
                l1->next->val += (l1->val)/10;
                l1->val = l1->val%10;
            }
            l1 = l1->next;
        }
        ListNode* abc = new ListNode();
        if(l1->next == nullptr && l1->val>9){
            l1->next = abc;
            abc->val = (l1->val)/10;
            l1->val = l1->val%10;

        }
        return node1;
    }
};

When the testcase value for l1 is [8,8,8] and l2 is [2,2], I get [0,1,9] which is the correct and expected result but if I modify the test cases to [8,8,6] and [2,2,2], I get the sum as [10,10,8]. Similarly, I am getting the wrong answer for l1 = [2,4,3] and l2 = [5,6,4] but I cannot figure out why.


Solution

  • There are a few issues:

    More comments can be made about your code, but here is the version that tackles the above mentioned points:

    class Solution {
    public:
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
            ListNode* node1 = l1;
            ListNode* node2 = l2;
            if(!node1 && !node2){
                return l1;
            }
            while (node1->next && node2->next){ // Stop when arriving at a tail node
                node1->val = node1->val + node2->val;
                node1 = node1->next;
                node2 = node2->next;    
            }
            node1->val = node1->val + node2->val; // Also update that node
            if(!node1->next){ // If the first list is the shortest
                node1->next = node2->next; // then attach the remaining nodes to it
            }
            node1 = l1;
            while(l1->next != nullptr){
                if(l1->val >9){
                    l1->next->val += (l1->val)/10;
                    l1->val = l1->val%10;
                }
                l1 = l1->next;
            }
            ListNode* abc = new ListNode();
            if(l1->val>9){
                l1->next = abc;
                abc->val = (l1->val)/10;
                l1->val = l1->val%10;
            }
            return node1;
        }
    };
    

    A next improvement would be to do join the two loops into one loop, so that you process the carry immediately when storing the sum.