Leetcode # 2: adding two numbers [Medium]

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42191317/article/details/102772706

description

We are given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit.

If we add up these two numbers, it will return a new list and to represent them.

You can assume that in addition to the numbers 0, these two numbers will not begin with 0.

Examples

Example:

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

Thinking

1> violent solution

Add together from start to finish, set the flag to represent whether to enter 1.

Time complexity O (M + N), the spatial complexity is O (1)

Precautions:

Note that some of the boundary conditions, such as with an inlet of exactly 10, as equal to the length of two lists, next are empty, but there are cases carry the like.

Code

package cn.lzx.linkedlist;

/**
 *@ClassNameLeetcode2_addTwoNumbers
 *@Description
 *@Author lzx
 *@Date2019/10/27 22:52
 *@Version V1.0
 **/
public class Leetcode2_addTwoNumbers {


    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
        }
    }


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }
        if(l2 == null){
            return l1;
        }
        ListNode res = new ListNode(0);
        ListNode first = res;
        //记录是否进位
        boolean flag = false;
        while(l1 != null && l2 != null){
            if(l1.val + l2.val + (flag ? 1 : 0) > 9){
                res.next = new ListNode((l1.val + l2.val + (flag ? 1 : 0)) % 10);
                flag = true;
            }else{
                res.next = new ListNode(l1.val + l2.val + (flag ? 1 : 0));
                flag = false;
            }
            res = res.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        //处理最后一次进位
        while(flag){
            if(l1 != null){
                //是否会再次进位
                if(l1.val + 1 > 9){
                    res.next = new ListNode((l1.val + 1)%10);
                }else{
                    flag = false;
                    res.next = new ListNode(l1.val + 1);
                }
                l1 = l1.next;
            }else if(l2 != null){
                if(l2.val + 1 > 9){
                    res.next = new ListNode((l2.val + 1)%10);
                }else{
                    flag = false;
                    res.next = new ListNode(l2.val + 1);
                }
                l2 = l2.next;
            }else{
                res.next = new ListNode(1);
                flag = false;
            }
            res = res.next;
        }
        while(l1 != null){
            res.next = new ListNode(l1.val);
            res = res.next;
            l1 = l1.next;
        }
        while(l2 != null){
            res.next = new ListNode(l2.val);
            res = res.next;
            l2 = l2.next;
        }
        return first.next;
    }

}

 

Guess you like

Origin blog.csdn.net/qq_42191317/article/details/102772706