/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode end = dummy;
int c = 0;
while(l1 != null && l2 != null) {
int s = l1.val + l2.val + c;
c = s / 10;
ListNode node = new ListNode(s % 10);
end = end.next = node;
l1 = l1.next;
l2 = l2.next;
}
while(l1 != null) {
int s = l1.val + c;
c = s / 10;
ListNode node = new ListNode(s % 10);
end = end.next = node;
l1 = l1.next;
}
while(l2 != null) {
int s = l2.val + c;
c = s / 10;
ListNode node = new ListNode(s % 10);
end = end.next = node;
l2 = l2.next;
}
if(c == 1) {
ListNode node = new ListNode(c);
end = end.next = node;
}
return dummy.next;
}
}