LeetCode - 两数相加

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/biezhihua/article/details/79437867

题目解析:https://leetcodechina.com/problems/add-two-numbers/description/

https://github.com/biezhihua/LeetCode

核心解析:
1. 链表对应结点相加时增加前一个结点的进位,并保存下一个结点的进位;除法得进位,模得结果。
2. 两个链表长度不一致时,要处理较长链表剩余的高位和进位计算的值;
3. 如果最高位计算时还产生进位,则还需要添加一个额外结点。

最佳解法:

ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode tmp = null;
    ListNode result = null;

    int carry = 0;
    while (l1 != null || l2 != null || carry != 0) {
        int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
        carry = sum / 10;

        ListNode node = new ListNode(sum % 10);
        if (tmp == null) {
            tmp = node;
            result = tmp;
        } else {
            tmp.next = node;
            tmp = tmp.next;
        }

        l1 = l1 == null ? null : l1.next;
        l2 = l2 == null ? null : l2.next;
    }

    return result;
}

public class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

我的解法:

/**
 * https://leetcodechina.com/problems/add-two-numbers/description/
 */
public class Code_3_4_AddTwoNumbers {

    @Test
    public void test() {
        ListNode _342 = new ListNode(2);
        _342.next = new ListNode(4);
        _342.next.next = new ListNode(3);

        ListNode _465 = new ListNode(5);
        _465.next = new ListNode(6);
        _465.next.next = new ListNode(4);

        ListNode result = addTwoNumbers(_342, _465);
        Assert.assertEquals(7, result.val);
        Assert.assertEquals(0, result.next.val);
        Assert.assertEquals(8, result.next.next.val);


        ListNode result1 = addTwoNumbers(new ListNode(5), new ListNode(5));
        Assert.assertEquals(0, result1.val);
        Assert.assertEquals(1, result1.next.val);


        ListNode _98 = new ListNode(9);
        _98.next = new ListNode(8);

        ListNode _1 = new ListNode(1);
        ListNode result2 = addTwoNumbers(_98, _1);
        Assert.assertEquals(0, result2.val);
        Assert.assertEquals(9, result2.next.val);
    }

    ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode tmp = null;
        ListNode result = null;


        boolean flag = false;

        while (l1 != null && l2 != null) {

            int cur = l1.val + l2.val + (flag ? 1 : 0);

            flag = cur >= 10;

            ListNode node = new ListNode(flag ? cur % 10 : cur);
            if (tmp == null) {
                tmp = node;
                result = tmp;
            } else {
                tmp.next = node;
                tmp = tmp.next;
            }

            l1 = l1.next;
            l2 = l2.next;
        }

        while (l1 != null) {

            int cur = l1.val + (flag ? 1 : 0);

            flag = cur >= 10;

            ListNode node = new ListNode(flag ? cur % 10 : cur);
            if (tmp == null) {
                tmp = node;
                result = tmp;
            } else {
                tmp.next = node;
                tmp = tmp.next;
            }

            l1 = l1.next;

        }

        while (l2 != null) {

            int cur = l2.val + (flag ? 1 : 0);

            flag = cur >= 10;

            ListNode node = new ListNode(flag ? cur % 10 : cur);
            if (tmp == null) {
                tmp = node;
                result = tmp;
            } else {
                tmp.next = node;
                tmp = tmp.next;
            }
            l2 = l2.next;
        }

        if (flag) {
            tmp.next = new ListNode(1);
        }

        return result;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页