# Add Two Numbers LeetCode Solution

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

## 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.

```Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:
Input: l1 = , l2 = 
Output: 

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 : Fig – Visualization of the addition of two numbers: 342 + 465 = 807Each 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 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);
}
}
```Code language: Java (java)```

## Add Two Numbers Leetcode Solution JavaScript->

``````var addTwoNumbers = function(l1, l2) {
let node = null
const carry = arguments
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):
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)```

## Nim Game LeetCode Solution

Given n, the number of stones in the heap, return true if you can win the game assuming…

## Regular Expression Matching LeetCode Solution

Given an input string s and a pattern p, implement regular expression matching with support for ‘.’ and ‘*’ where ‘.’ Matches any…

## Zigzag Conversion LeetCode Solution

The string PAYPALISHIRING is written in a zigzag pattern on a given number of rows…

## Basic Calculator II LeetCode Solution

Given a string s which represents an expression, evaluate this expression and return its value. The integer division…

## 3Sum LeetCode Solution

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i !=…

## House Robber LeetCode Solution

Given an integer array nums representing the amount of money of each house, return the…