两数相加题解思路

       耗时一小时,这道题一开始的思路和递归没有关系。这种题的边界条件非常耗时,对于不好处理的地方,应该要使用别的方法来代替处理才行。

 力扣

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode root = new ListNode();
        
        ListNode temp = root;
        ListNode last = temp;
        int carry = 0;
        while (l1 != null && l2 != null) {
            int sum = l1.val + l2.val + carry;
            if (sum < 10) {
                temp.val = sum;
                carry = 0;
            } else {
                int x = sum % 10;
                temp.val = x;
                carry = 1;
            }
            ListNode newNode = new ListNode(-1);
            last = temp;
            temp.next = newNode;
            temp = temp.next;
            
            l1 = l1.next;
            l2 = l2.next;
        }
        
        // l1和l2总有一个是等于null的
        // l1还没有结束,把l1的值加到root后面
        if (l1 != null) {
            while (l1 != null) {
                int singleSum = l1.val + carry;
                if (singleSum < 10) {
                    temp.val = singleSum;
                    carry = 0;
                } else {
                    int x = singleSum % 10;
                    temp.val = x;
                    carry = 1;
                }
                
                ListNode newNode = new ListNode(-1);
                last = temp;
                temp.next = newNode;
                temp = temp.next;
                
                l1 = l1.next;
            }
            if (carry == 1) {
                temp.val = carry;
            }
        } else {
            if (carry == 1) {
                temp.val = carry;
            }
        }
        
        // l2还没有结束,把l2的值加到root后面
        if (l2 != null) {
            while (l2 != null) {
                int singleSum = l2.val + carry;
                if (singleSum < 10) {
                    temp.val = singleSum;
                    carry = 0;
                } else {
                    int x = singleSum % 10;
                    temp.val = x;
                    carry = 1;
                }
                
                ListNode newNode = new ListNode(-1);
                last = temp;
                temp.next = newNode;
                temp = temp.next;
                
                l2 = l2.next;
            }
            if (carry == 1) {
                temp.val = carry;
            }
        } else {
            if (carry == 1) {
                temp.val = carry;
            }
        }
        
        if (temp.val == -1) {
            last.next = null;
        }
        
        return root;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值