Java accepted solution

public class Solution {

public ListNode addTwoNumbers(ListNode A, ListNode B) {
    ListNode dummy = new ListNode(0);
    ListNode result = dummy;
    int carry = 0;
    while(A != null && B != null){
        int sum = carry + A.val + B.val;
        carry = sum / 10;
        result.next = new ListNode(sum % 10);
        result = result.next;
        A = A.next;
        B = B.next;
    }
    ListNode node = null;
    if(null != A){
        node = A;
    }else if(null != B){
        node = B;
    }
    while(node != null){
        int sum = carry + node.val;
        carry = sum / 10;
        result.next = new ListNode(sum % 10);
        result = result.next;
        node = node.next;
    }
    if(carry > 0){
        result.next = new ListNode(carry);
    }
    return dummy.next;
}

}

Click here to start solving coding interview questions