/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists(ListNode l1, ListNode l2) {
if(l1 == null && l2 == null){
return null;
}
ListNode dummy = new ListNode(0);
ListNode point = dummy;
int carry = 0;
while(l1 != null && l2 != null){
int sum = carry + l1.val + l2.val;
point.next = new ListNode(sum%10);
carry = sum / 10;
l1 = l1.next;
l2 = l2.next;
point = point.next;
}
while(l1 != null){
int sum = carry + l1.val;
point.next = new ListNode(sum%10);
carry = sum / 10;
l1 = l1.next;
point = point.next;
}
while(l2 != null){
int sum = carry + l2.val;
point.next = new ListNode(sum%10);
carry = sum / 10;
l2 = l2.next;
point = point.next;
}
if(carry != 0){
point.next = new ListNode(carry);
point = point.next;
}
return dummy.next;
}
}