力扣(LeetCode)算法题(二) 两数相加

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我的参考答案

public class Solution {
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode o= l1;
        ListNode p= l2;
        
        ListNode newListNode = new ListNode(0);
       
        if(o==null&&p==null){
            return newListNode;
        }
        if(o==null&&p!=null){
            return p;
        }
        if(p==null&&o!=null){
            return o;
        }

        ListNode curr = newListNode;
        int c = 0;
        int x=0;
        int y=0;
        int sum = 0;
        while (o!=null&&p!=null){
            x=o.getVal();
            y=p.getVal();
            sum=x+y+c;
            if(sum>=10) {
                sum=x+y+c-10;                
                c=1;
            } else{
                sum = x+y+c;                
                c=0;
            } 
            curr.setNext(new ListNode(sum));
            curr = curr.getNext();
            o=o.getNext();
            p=p.getNext();
        }
        if(o==null&&p!=null)
        {
            if(c==0){
                curr.setNext(p);
            }
            else{
                while (p!=null){
                    y=p.getVal();
                    sum=y+c;
                    if(sum>=10) {
                        sum=y+c-10;                
                        c=1;
                    } else{
                        sum = y+c;                
                        c=0;
                    } 
                    curr.setNext(new ListNode(sum));
                    curr = curr.getNext();
                    p=p.getNext();
                }
            }
        }

        if(o!=null&&p==null)
        {
            if(c==0){
                curr.setNext(o);
            }
            else{
                while (o!=null){
                    y=o.getVal();
                    sum=y+c;
                    if(sum>=10) {
                        sum=y+c-10;                
                        c=1;
                    } else{
                        sum = y+c;                
                        c=0;
                    } 
                    curr.setNext(new ListNode(sum));
                    curr = curr.getNext();
                    o=o.getNext();
                }
            }
        }

        if(p==null&&o==null&&c==1){
            curr.setNext(new ListNode(1));
        }
        return newListNode.getNext();
    }
}

 

我的测试代码:

public class TestAddTwoNumbers {

	public static void main(String[] args) {
		ListNode n1= new ListNode(9);
		ListNode n21= new ListNode(1);
		ListNode n22= new ListNode(8);
		n21.setNext(n22);
		Solution s = new Solution();
		ListNode r = s.addTwoNumbers(n1, n21);
		System.out.println("r.val==="+r.getVal());
		System.out.println("r.next.val==="+r.getNext().getVal());
		
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值