# Accepted Java solution: beats 96.95% of the solutions:-

• Basically, reverse both the lists, add them and return the reversed list.
This is my first post here on leetcode. All comments are welcomed :)

``````public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

int carry = 0;
ListNode resultList = new ListNode(0);
ListNode temp = resultList;

l1 = reverse(l1);
l2 = reverse(l2);
while(l1 != null || l2 != null || carry == 1){

int result = 0;

if(l1 == null && l2 == null){
temp.next = new ListNode(carry);
resultList = reverse(resultList.next);
return removeLast(resultList);
}
if(l1 != null) result += l1.val;
if(l2 != null) result += l2.val;
result += carry;
carry = 0;
if(result > 9){
carry = 1;
result = result - 10;
}

ListNode node = new ListNode(result);
temp.next = node;
temp = temp.next;
if(l1 != null)
l1 = l1.next;
if(l2 != null)
l2 = l2.next;

}

resultList = reverse(resultList.next);
return removeLast(resultList);

}

while(node.next != null){

node = node.next;
}
node = node.next;
}

private ListNode reverse(ListNode node){

ListNode current = node;
ListNode next = null;
ListNode prev = null;

while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;

}
}
``````

• This post is deleted!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.