# Add Two Numbers II LeetCode Solution

Here, We see Add Two Numbers II 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

## Problem Statement

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]

Example 2:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [8,0,7]

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

## Add Two Numbers II LeetCode SolutionC++

``````class Solution {
public:
ListNode* prev = NULL;
}
return prev;
}

ListNode* Helper(ListNode* l1, ListNode* l2) {
int carry = 0;
while (l1 != nullptr || l2 != nullptr || carry != 0) {
int digit1 = (l1 != nullptr) ? l1->val : 0;
int digit2 = (l2 != nullptr) ? l2->val : 0;
int sum = digit1 + digit2 + carry;
int digit = sum % 10;
carry = sum / 10;
ListNode* newNode = new ListNode(digit);
tail->next = newNode;
tail = tail->next;
l1 = (l1 != nullptr) ? l1->next : nullptr;
l2 = (l2 != nullptr) ? l2->next : nullptr;
}
return result;
}

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
l1 = reverseList(l1);
l2 = reverseList(l2);
ListNode* ans = Helper(l1, l2);
return reverseList(ans);
}
};```Code language: PHP (php)```

## Add Two Numbers II LeetCode SolutionJava

``````class Solution {
ListNode prev = null;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}

public ListNode helper(ListNode l1, ListNode l2) {
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
int digit1 = (l1 != null) ? l1.val : 0;
int digit2 = (l2 != null) ? l2.val : 0;
int sum = digit1 + digit2 + carry;
int digit = sum % 10;
carry = sum / 10;
ListNode newNode = new ListNode(digit);
tail.next = newNode;
tail = tail.next;
l1 = (l1 != null) ? l1.next : null;
l2 = (l2 != null) ? l2.next : null;
}
return result;
}

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1 = reverseList(l1);
l2 = reverseList(l2);
ListNode ans = helper(l1, l2);
return reverseList(ans);
}
}```Code language: PHP (php)```

## Add Two Numbers II SolutionJavaScript

``````var addTwoNumbers = function(l1, l2) {
let stack1 = [];
let stack2 = [];
while(l1) {
stack1.push(l1.val);
l1 = l1.next;
}
while(l2) {
stack2.push(l2.val);
l2 = l2.next;
}
let l3 = new ListNode(0);
while(stack1.length || stack2.length) {
let sum = 0;
if(stack1.length) sum += stack1.pop();
if(stack2.length) sum += stack2.pop();
sum += l3.val;
l3.val = sum%10;
}
return (l3.val === 0) ? l3.next : l3;
};```Code language: JavaScript (javascript)```

## Add Two Numbers II SolutionPython

``````class Solution(object):
prev = None
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prev

def helper(self, l1, l2):
carry = 0
while l1 or l2 or carry:
digit1 = l1.val if l1 else 0
digit2 = l2.val if l2 else 0
total = digit1 + digit2 + carry
digit = total % 10
carry = total // 10
newNode = ListNode(digit)
tail.next = newNode
tail = tail.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None