# Add Two Numbers LeetCode Solution

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

## 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 current node to dummy head of the returning list.
• Initialize carry to 0.
• Initialize p and q to head of l1 and l2 respectively.
• Loop through lists l1 and l2 until you reach both ends.
• Set x to 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 current node’s next, then advance current node to next.
• Advance both p and q.
• Check if carry=1, if so append a new node with digit 1 to the returning list.
• Return dummy head’s next node.

## C++ Solution ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```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)```

## Java Solution ->

``````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);
}
}
```Code language: Java (java)```

## JavaScript Solution ->

``````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)```

## Python Solution ->

``````class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
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)```

Add Two Numbers, Add Two Numbers Leetcode, Add Two Numbers Leetcode solution, leetcode Add Two Numbers solution, Leetcode Add Two Numbers, Add Two Numbers python solution, Add Two Numbers java solution, Add Two Numbers JavaScript solution, Add Two Numbers C++ solution

## Longest Substring Without Repeating Characters LeetCode Solution

Given a string s, find the length of the longest substring without repeating characters.

## Remove Duplicates from Sorted Array

Here, We see Remove Duplicates from Sorted Array problem Solution. This Leetcode problem done in…

## Zigzag Conversion LeetCode Solution

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

## Best Time to Buy and Sell Stock II

Here, We see Best Time to Buy and Sell Stock II problem Solution. This Leetcode problem…

## Median of Two Sorted Arrays LeetCode Solution

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time…

## Two Sum LeetCode Solution

Given an array of integers nums and an integer target, return indices of the two…