leetcode_2 用两个链表表示两个整数并相和

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
   public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(0);
        ListNode next=null;
        ListNode next1 = l1;
        ListNode next2 = l2;
        int x=0;
        for (; next2 != null || next1 != null;){
            if (next1 != null && next2 == null){
                next.next=next1;
                if (x==1){
                    next = next.next;
                    while (true){
                        int val_result = next.val+x;
                        if (val_result<10){
                            next.val=val_result;
                            x = 0;
                            break;
                        }else{
                            next.val=0;
                            // x = 1;
                            if (next.next==null){
                                break;
                            }
                            next=next.next;
                        }

                    }
                }
                break;
            }else if (next2 != null && next1 == null){
                next.next=next2;
                if (x==1){
                    next = next.next;
                    while (true){
                        int val_result = next.val+x;
                        if (val_result<10){
                            next.val=val_result;
                            x = 0;
                            break;
                        }else{
                            next.val=0;
                            // x = 1;
                            if (next.next==null){
                                break;
                            }
                            next=next.next;
                        }

                    }
                }
                break;
            }
            int val_result = next1.val+next2.val+x;
            if (next==null){
                next = result;
                if(val_result>=10){
                    next.val = val_result-10;
                    x = 1;
                }else{
                    next.val = val_result;
                    x = 0;
                }
            }else{
                if(val_result>=10){
                    next.next = new ListNode(val_result-10);
                    x = 1;
                }else{
                    next.next = new ListNode(val_result);
                    x = 0;
                }
                next = next.next;
            }
            next1=next1.next;
            next2=next2.next;

        }
        if (x==1){
            next.next=new ListNode(1);
        }
        return result;

    }

}

我的收获有:

  1. 局部变量不会被初始化(全局变量会被初始化):
public class test {
    
    public static void main(String[] args) {
        ListNode i;
        if (i==null){
            System.out.println("null");
        }
    }
}

这个i=null编译器会报错的,因为i是一个局部变量,没用初始化,所以没办法执行i==null
但是:

public class test {
    private static ListNode i;
    public static void main(String[] args) {
        if (i==null){
            System.out.println("null");
        }
    }
}

这样是可以的,全局变量(没static也可以)会被初始化为null,int会被初始化为0,boolean会被初始化为false等。。。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值