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;
}
}

posted @ 2022-07-12 01:33  flag!  阅读(18)  评论(0)    收藏  举报