链表相加
/**
* 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 head = new ListNode();
ListNode cur = head;
int leftValue = 0;
while(l1!=null||l2!=null||leftValue!=0){
if(l1!=null){
leftValue+=l1.val;
l1 = l1.next;
}
if(l2!=null){
leftValue+=l2.val;
l2 = l2.next;
}
cur.next = new ListNode(leftValue%10);
cur = cur.next;
leftValue /=10;
}
return head.next;
}
}