当前位置 : 首页 » 文章分类 :  算法  »  LeetCode.002.Add Two Numbers 两数相加

LeetCode.002.Add Two Numbers 两数相加

题目描述

2 Add Two Numbers
https://leetcode-cn.com/problems/add-two-numbers/

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

解题过程

相似题目
LeetCode.002.Add Two Numbers 两数相加
LeetCode.445.Add Two Numbers II 两数相加 II

也是之前做过的题,没啥复杂的算法,就是写起来比较繁琐。
这道题我写起来非常顺利,一气呵成,写完一测紧接着提交就AC,连调试都没调试。

时间复杂度 O(max(m,n)),空间复杂度 O(max(m,n)),m,n分别是两个链表的长度

private static class SolutionV2018 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null && l2 == null) {
            return null;
        }
        if (l1 == null || l2 == null) {
            return l1 == null ? l2 : l1;
        }
        ListNode head = new ListNode(0);//值无用的头结点
        ListNode prev = head;//记住上一个结点
        int carry = 0;//进位

        //l1,l2都未结束
        for (; l1 != null && l2 != null; l1 = l1.next, l2 = l2.next) {
            ListNode temp = new ListNode((l1.val + l2.val + carry) % 10);
            carry = (l1.val + l2.val + carry) / 10;
            prev.next = temp;
            prev = prev.next;
        }

        //l1,l2有一个结束,但还有进位,还需要申请新结点
        while (carry != 0) {
            int sum = carry;
            if (l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            ListNode temp = new ListNode(sum % 10);
            carry = sum / 10;
            prev.next = temp;
            prev = prev.next;
        }

        //无进位了,只需将剩下的节点连在结果后面
        prev.next = l1 != null ? l1 : l2;
        return head.next;
    }
}

提交后看讨论版,有的大牛给出的代码是真的简洁又漂亮,比如下面这个:

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode c1 = l1;
        ListNode c2 = l2;
        ListNode dummyhead = new ListNode(0);
        ListNode d = dummyhead;
        int sum = 0;
        while( c1 != null || c2 != null){
            sum = sum / 10;
            if(c1 != null){
                sum += c1.val;
                c1 = c1.next;
            }
            if(c2 != null){
                sum += c2.val;
                c2 = c2.next;
            }
            d.next = new ListNode(sum % 10);
            d = d.next;
        }
        if(sum >= 10) d.next = new ListNode(1);
        return dummyhead.next;
    }
}

也就是只要l1和l2中有一个还没结束,就能在一个循环中处理,两个都结束后如果有进位只可能是1,在最后再挂一个结点即可

我以为这样就很简洁了,没想到还有个更集成化的:

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode prev = new ListNode(0);
        ListNode head = prev;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            ListNode cur = new ListNode(0);
            int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry;
            cur.val = sum % 10;
            carry = sum / 10;
            prev.next = cur;
            prev = cur;

            l1 = (l1 == null) ? l1 : l1.next;
            l2 = (l2 == null) ? l2 : l2.next;
        }
        return head.next;
    }
}

只要l1非空、l2非空、进位不为0三个条件中任意一个满足,就继续循环,在循环内如果链表为空用0代替


GitHub代码

algorithms/leetcode/leetcode/_002_AddTwoNumbers.java
https://github.com/masikkk/algorithms/blob/master/leetcode/leetcode/_002_AddTwoNumbers.java


上一篇 LeetCode.003.Longest Substring Without Repeating Characters 最长非重复子串

下一篇 LeetCode.001.Two Sum 两数之和

阅读
评论
703
阅读预计3分钟
创建日期 2018-02-03
修改日期 2018-02-03
类别

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论