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 contain a single digit. Add the two numbers and return it as a linked list.

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

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
}
}

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int num1, num2, num0;
ListNode l = null;

if (l1 != null && l2 != null &&
l1.next != null && l2.next != null &&
l1.next.next != null && l2.next.next != null) {
num1 = l1.val + l1.next.val * 10 + l1.next.next.val * 100;
num2 = l2.val + l2.next.val * 10 + l2.next.next.val * 100;
num0 = num1 + num2;
l = new ListNode(num0 % 10);
l.next = new ListNode((num0 / 10) % 10);
l.next.next = new ListNode(num0 / 100);
}
return l;
}
}

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int num1 = 0, num2 = 0, num0;
int i = 0;
while (l1 != null) {
num1 += l1.val * Math.pow(10, i);
i++;
l1 = l1.next;
}
int j = 0;
while (l2 != null) {
num2 += l2.val * Math.pow(10, j);
j++;
l2 = l2.next;
}
num0 = num1 + num2;

ListNode l = null;
l = new ListNode(num0 % 10);
l.next = new ListNode((num0 / 10) % 10);
l.next.next = new ListNode(num0 / 100);
return l;
}
}

9 / 1562 test cases passed.
Submitted: 0 minutes ago
Input:
[0]
[0]
Output:
[0,0,0]
Expected:
[0]

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int num1 = 0, num2 = 0, num0;
int i = 0;
while (l1 != null) {
num1 += l1.val * Math.pow(10, i);
i++;
l1 = l1.next;
}
int j = 0;
while (l2 != null) {
num2 += l2.val * Math.pow(10, j);
j++;
l2 = l2.next;
}
num0 = num1 + num2;

ListNode l = null;
if (num0 == 0) {
l = new ListNode(0);
} else {
l = new ListNode(num0 % 10);
l.next = new ListNode((num0 / 10) % 10);
l.next.next = new ListNode(num0 / 100);
}
return l;
}
}

10 / 1562 test cases passed.
Submitted: 0 minutes ago
Input:
[0]
[1]
Output:
[1,0,0]
Expected:
[1]

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int num1 = 0, num2 = 0, num0;
int i = 0;
while (l1 != null) {
num1 += l1.val * Math.pow(10, i);
i++;
l1 = l1.next;
}
int j = 0;
while (l2 != null) {
num2 += l2.val * Math.pow(10, j);
j++;
l2 = l2.next;
}
num0 = num1 + num2;

ListNode l = new ListNode(0);
while (num0 != 0) {
l.val = num0 % 10;
num0 /= 10;
if (num0 != 0) {
l.next = new ListNode(0);
l = l.next;
} else {
l.next = null;
}
}
}
}

787 / 1562 test cases passed.
Submitted: 0 minutes ago
Input:
[9]
[1,9,9,9,9,9,9,9,9,9]
Output:
[0,-4,-6,-3,-8,-4,-7,-4,-1,-2]
Expected:
[0,0,0,0,0,0,0,0,0,0,1]

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode l = new ListNode(0);
int sum = 0, tmp = 0;
//l1 : 9 9
//l2 : 1 9 9 9
//l.next : 0 9 0 0 1
while (l1 != null && l2 != null) {
sum = l1.val + l2.val + tmp;
l.next = new ListNode(sum % 10);
tmp = sum / 10;
l = l.next;
l1 = l1.next;
l2 = l2.next;
}

while (l2 != null) {
sum = l2.val + tmp;
l.next = new ListNode(sum % 10);
tmp = sum / 10;
l = l.next;
l2 = l2.next;
}

while (l1 != null) {
sum =l1.val + tmp;
l.next = new ListNode(sum % 10);
tmp = sum / 10;
l = l.next;
l1 = l1.next;
}

if (tmp == 1) {
l.next = new ListNode(tmp);
}

}
}

Submission Detail
1562 / 1562 test cases passed.
Status: Accepted
Runtime: 69 ms
Submitted: 0 minutes ago

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null || l2 == null) {
return l1 == null ? l2 : l1;
}
int value = l1.val + l2.val;
ListNode result = new ListNode(value % 10);