两个链表对应数字相加
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1==null||l2==null){
return l2==null? l1: l2;
}
ListNode head = new ListNode(-1);
ListNode temp=head;
int jinwei=0;
while(l1!=null||l2!=null){
int l1Value = l1 !=null ? l1.val : 0;
int l2Value = l2 !=null ? l2.val : 0;
ListNode node = new ListNode();
node.val = (l1Value+l2Value+jinwei)%10;
jinwei = (l1Value+l2Value+jinwei)/10;
temp.next = node;
temp = node;
if(l1!=null){
l1=l1.next;
}
if(l2!=null){
l2=l2.next;
}
}
if(jinwei!=0){
ListNode node = new ListNode(jinwei);
temp.next=node;
}
return head.next;
}
}