首页 > 其他 > 详细

[LeetCode]Add Two Numbers

时间:2019-05-17 21:20:12      阅读:123      评论:0      收藏:0      [点我收藏+]

 

技术分享图片

 

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode head=new ListNode(0),temp=head;
    int carry = 0;
    while (l1!=null||l2!=null) {
        int x=(l1!=null)?l1.val:0;
        int y=(l2!=null)?l2.val:0;
        int sum=x+y+carry;
        carry=sum/10;
        temp.next=new ListNode(sum%10);
        temp=temp.next;
        if (l1!=null) l1=l1.next;
        if (l2!=null) l2=l2.next;
    }
    if(carry>0)
        temp.next=new ListNode(carry);
    
    return head.next;
}
}

 

[LeetCode]Add Two Numbers

原文:https://www.cnblogs.com/xybz/p/10883666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!