LEETCODE2 add two numbers
作者:互联网
so simple for this problem, but when I review it ,there are sill some main points which should be noticed.
1. dummy
2. carray.
3. after while l1,l2 != nulll or carry=0
4.
public ListNode addTwoNumbers(ListNode l1,ListNdoe l2){
ListNode dummy = new ListNode(0);
ListNode cur=dummy;
int carry=0;
while(l1 !=null||l2!=null||carry!=0){
int x=(l1 !=null)? l1.val:0;
int y=(l2!=null)? l2.val:0;
int sum=carry+x+y;
carry=sum/10;
cur.next= new ListNode(sum%10);
cur=cur.next;
if(l1!=null)l1=l1.next;
if(l2!=null)l2=l2.next;
}
return dummy.next;
}
}
标签:LEETCODE2,two,next,add,l2,l1,carry,ListNode,null 来源: https://www.cnblogs.com/LLflag1/p/16468569.html