题目
解答
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int temp = 0;
ListNode root = new ListNode(0);
ListNode cur = root;
int carry = 0;
while(l1!=null||l2!=null||carry>0){
int v1 = l1==null?0:l1.val;
int v2 = l2==null?0:l2.val;
temp = v1+v2+carry;
ListNode sumNode = new ListNode(temp%10);
cur.next = sumNode;
cur = cur.next;
carry = temp/10;
if(l1!=null) l1=l1.next;
if(l2!=null) l2=l2.next;
}
return root.next;
}
}