LeetCode 2.两数相加

public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    int j=0,temp=0,u;
    ListNode result=null,now=null;
    while(l1!=null||l2!=null){
        System.out.println("j"+j);
        System.out.println("l1"+l1.val);
        System.out.println("l2"+l2.val);
        if(l1==null){
            temp=l2.val;
        }else if(l2==null){
            temp=l1.val;
        }else{
            temp=l1.val+l2.val;
        }
        if(j==1) {
            temp++;
            j=0;
        }

        if(temp>9){
            j=1;
            temp=temp-10;
        }
        if(result==null){
            result=new ListNode(temp);
            now=result;
        }else{
            now.next=new ListNode(temp);
            now=now.next;
        }
        if(l1!=null){
            l1=l1.next;
        }
        if(l2!=null) {
            l2 = l2.next;
        }
        System.out.println("yyy");

    }

    if(j==1){
        now.next=new ListNode(1);
    }

    return result;
}

你可能感兴趣的:(LeetCode)