In this Leetcode Add Two Numbers problem solution we have given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

Leetcode Add Two Numbers problem solution


Problem solution in Python.

class Solution:
    def insert(self,l,v):
        if l==None:
            l.val=v
        else:
            r=l
            while r.next!=None:
                r=r.next
            tmp=ListNode(v)
            r.next=tmp
    def addTwoNumbers(self, l1, l2):
        res=ListNode(0)
        s,s2='',''
        while l1!=None:
            s+=str(l1.val)
            l1=l1.next
        while l2!=None:
            s2+=str(l2.val)
            l2=l2.next
        s=int(s[::-1])+int(s2[::-1])
        for x in str(s)[::-1]:
            self.insert(res,int(x))
        return res.next



Problem solution in Java.

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0, sum;
        ListNode head = new ListNode(0);
        ListNode ln1 = l1, ln2 = l2, node = head;
        
        while (carry != 0 || ln1 != null || ln2 != null) {
            sum = carry; // reset sum to value of carry
            
            if (ln1 != null) {
                sum += ln1.val;
                ln1 = ln1.next;
            }
            if (ln2 != null) {
                sum += ln2.val;
                ln2 = ln2.next;
            }
            
            carry = sum / 10;
            node.next = new ListNode(sum % 10);
            node = node.next; 
        }
        return head.next;    
    }


Problem solution in C++.

class Solution {
public:
    
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int val_1 , val_2, new_val, remainder;
        ListNode* not_null;
        ListNode* maybe_null;
        
        if(l1 == NULL && l2 == NULL){
            return NULL;
        }
        
        val_1 = (l1 == NULL? 0: l1->val);
        val_2 = (l2 == NULL? 0: l2->val);
        
        not_null = (l1 == NULL? l2: l1);
        maybe_null = ((l1 == not_null)? l2:l1);
        
        not_null->val= val_1+val_2;
        not_null->next = addTwoNumbers(not_null->next, (maybe_null == NULL?NULL:maybe_null->next));
        
        remainder = floor(not_null->val/10);
        not_null->val = (not_null->val%10);
        ListNode* this_node= not_null;
        
        while(remainder != 0 && this_node->next!=NULL){
            (this_node->next->val)+=remainder;
            remainder= floor(this_node->next->val/10);
            (this_node->next->val) = (this_node->next->val%10);
            this_node = this_node->next;
        }
        
        if(remainder!= 0 && this_node->next==NULL){
            this_node->next = new ListNode(remainder);
        }
        
        return not_null;
    }
};


Problem solution in C.

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    struct ListNode *p = l1, *q = l2;
    struct ListNode *result = NULL, *curr = NULL;
    int carry = 0;
    while (p != NULL || q != NULL || carry != 0) {
        struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
        int x = (p != NULL) ? p->val : 0;
        int y = (q != NULL) ? q->val : 0;
        int sum = (carry + x + y) % 10;
        carry = (carry + x + y) / 10;
        newNode->val = sum;
        newNode->next = NULL;
        if (result == NULL) {
            result = curr = newNode;
        } else {
            curr->next = newNode;
            curr = newNode;
        }
        if (p != NULL) p = p->next;
        if (q != NULL) q = q->next;
    }
    return result;
}