Add Two Numbers LeetCode Solution

Here, We see Add Two Numbers LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

List of all LeetCode Solution

Add Two Numbers LeetCode Solution

Add Two Numbers LeetCode Solution

Problem Statement

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.

Add Two Numbers LeetCode Solution example
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Algorithm :

Add Two Numbers LeetCode Solution algorithm
Fig – Visualization of the addition of two numbers: 342 + 465 = 807
Each node contains a single digit and the digits are stored in reverse order.

Pseudocode :

  • Initialize the current node to the dummy head of the returning list.
  • Initialize carry to 0.
  • Initialize p and q to the head of l1 and l2 respectively.
  • Loop through lists l1 and l2 until you reach both ends.
    • Set x to the node p‘s value. If pp has reached the end of l1, set to 0.
    • Set y to node q‘s value. If q has reached the end of l2, set to 0.
    • Set sum = x+y+carry.
    • Update carry=sum/10.
    • Create a new node with the digit value of (sum mod 10) and set it to the current node’s next, then advance the current node to the next.
    • Advance both p and q.
  • Check if carry=1, if so append a new node with digit 1 to the returning list.
  • Return the dummy head’s next node.

Add Two Numbers Leetcode Solution C++

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int sum=0;
        ListNode *l3=NULL;
        ListNode **node=&l3;
        while(l1!=NULL||l2!=NULL||sum>0)
        {
            if(l1!=NULL)
            {
                sum+=l1->val;
                l1=l1->next;
            }
            if(l2!=NULL)
            {
                sum+=l2->val;
                l2=l2->next;
            }
            (*node)=new ListNode(sum%10);
            sum/=10;
            node=&((*node)->next);
        }        
        return l3;
    }
};
Code language: C++ (cpp)

Add Two Numbers Leetcode Solution Java

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummyHead = new ListNode(0);
    ListNode p = l1, q = l2, curr = dummyHead;
    int carry = 0;
    while (p != null || q != null) {
        int x = (p != null) ? p.val : 0;
        int y = (q != null) ? q.val : 0;
        int sum = carry + x + y;
        carry = sum / 10;
        curr.next = new ListNode(sum % 10);
        curr = curr.next;
        if (p != null) p = p.next;
        if (q != null) q = q.next;
    }
    if (carry > 0) {
        curr.next = new ListNode(carry);
    }
    return dummyHead.next;
}
Code language: Java (java)

Add Two Numbers Leetcode Solution JavaScript

var addTwoNumbers = function(l1, l2) {
    let node = null
    const carry = arguments[2]
    if (l1 || l2) {
        const val1 = l1 ? l1.val : 0
        const val2 = l2 ? l2.val : 0
        const next1 = l1 ? l1.next : null
        const next2 = l2 ? l2.next : null
        const val = carry ? val1 + val2 + 1 : val1 + val2
        node = new ListNode(val % 10)
        node.next = addTwoNumbers(next1, next2, val >= 10)  
    } else if (carry) {
        node = new ListNode(1)
        node.next = null
    }
    return node
};
Code language: JavaScript (javascript)

Add Two Numbers Leetcode Solution Python

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        result = ListNode(0)
        result_tail = result
        carry = 0
                
        while l1 or l2 or carry:            
            val1  = (l1.val if l1 else 0)
            val2  = (l2.val if l2 else 0)
            carry, out = divmod(val1+val2 + carry, 10)    
                      
            result_tail.next = ListNode(out)
            result_tail = result_tail.next                      
            
            l1 = (l1.next if l1 else None)
            l2 = (l2.next if l2 else None)
               
        return result.next
Code language: Python (python)
Scroll to Top