I'm a C++ developer by trade but am doing these exercises in an attempt to gain competence in Java.

I have developed a solution to this problem but it only beats 31.63% of javasubmissions, however to me it looks functionally identical to 'UpTheHell's solution which reportedly beats 98% of solutions. I'm just wondering what it is that is so expensive in my solution.

My solution:

```
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Quick Return if only 1 number
if(l1==null) return l2;
if(l2==null) return l1;
// Dummy node used to start the list
ListNode newNode = new ListNode(-1);
ListNode rv = newNode;
// Declare variables before loop
int carry=0, digitSum;
// Loop over all input digits
while (l1 != null || l2 != null || carry != 0)
{
digitSum=carry;
if (l1 != null)
{
digitSum += l1.val;
l1 = l1.next;
}
if (l2 != null)
{
digitSum += l2.val;
l2 = l2.next;
}
newNode.next = new ListNode(digitSum % 10);
carry = digitSum / 10;
newNode = newNode.next;
}
// Return node after dummy
return rv.next;
}
}
```

UpTheHell's ssolution:

```
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1==null) return l2;
if(l2==null) return l1;
int carry=0;
ListNode fakeHead=new ListNode(9); // add all node at the tail of fakeHead
ListNode tail=fakeHead;
while(carry>0 || l1!=null || l2!=null){ //check if next node exsits
int n1=l1==null?0:l1.val; // get value of number 1
l1=l1.next;
int n2=l2==null?0:l2.val; // get value of number 2
if(l2!=null)
l2=l2.next;
int sum=n1+n2+carry;
carry=sum/10; // calculate carry
ListNode cur=new ListNode(sum%10);
tail.next=cur;
tail=cur; // add node at the end of tail
}
return fakeHead.next;
}
}
```