Add Two Numbers Problem

Difficulty: Medium

Add Two Numbers: You are 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: l1 = [2->4->3] + l2 = [5->6->4]

Output: [7,0,8]

Explanation: 342 + 465 = 807.

Add Two Numbers Problem Solution

Python Solution

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

Java Solution

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;    
    }

C Solution

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;
}

Companies: Adobe, Aetion, Airbnb, Alibaba, Amazon, Apple, Baidu, Bloomberg, Cisco, Coursera, ebay, facebook, Google, Hulu, IBM, etc.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment