leetCode刷题 | 两数相加

简介: leetCode刷题 | 两数相加

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。


如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。


示例:


输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)

输出:7 -> 0 -> 8

原因:342 + 465 = 807


我的解法:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  int forward = 0;
        ListNode first = l1;
        ListNode second = l2;
        ListNode target = null;
        ListNode ret = null;
        while (true) {
            if (first == null) {
                first = new ListNode(0);
            }
            if (second == null) {
                second = new ListNode(0);
            }
            int i = first.val + second.val;
            int i1 = (i + forward) % 10;
            if (target == null) {
                ret = target = new ListNode(i1);
            } else {
                target.next = new ListNode(i1);
                target = target.next;
            }
            forward = (i + forward) / 10;
            if (first.next == null && second.next == null) {
                if (forward != 0) {
                    target.next = new ListNode(forward);
                }
                break;
            }
            first = first.next;
            second = second.next;
        }
        return ret;
    }
}

官方题解:

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummyHead = new ListNode(0);
    ListNode p = l1, q = l2, curr = dummyHead;
    int carry = 0;
    while (p != null || q != null) {
        int x = (p != null) ? p.val : 0;
        int y = (q != null) ? q.val : 0;
        int sum = carry + x + y;
        carry = sum / 10;
        curr.next = new ListNode(sum % 10);
        curr = curr.next;
        if (p != null) p = p.next;
        if (q != null) q = q.next;
    }
    if (carry > 0) {
        curr.next = new ListNode(carry);
    }
    return dummyHead.next;
}

比较我的解法跟官方的解法可以发现


1.官方使用了一个哑节点来统一第一个节点跟非第一个节点的情况,这一点做的非常简洁

官方在循环的条件控制上更加简洁,只有两个链表的下一个节点都为null的情况结束循环,同时需要判断进位是否大于0

时间复杂度都是一样的O(max(m,n))

还是官方牛b!!!


另外看到有的同学用递归也做了对应的解,这里也学习下:


递归解法:

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        return addTwoNumbers(l1, l2, null);
    }
    public ListNode addTwoNumbers(ListNode l1, ListNode l2, ListNode prev) {
        ListNode next1 = null;
        ListNode next2 = null;
        int val1 = 0;
        int val2 = 0;
        if (l1 != null) {
            val1 = l1.val;
            next1 = l1.next;
        }
        if (l2 != null) {
            val2 = l2.val;
            next2 = l2.next;
        }
        ListNode newNode = new ListNode(val1 + val2);
        if (prev != null) {
            if (prev.val >= 10) {
                prev.val %= 10;
                newNode.val += 1;
            }
        }
        if (next1 != null || next2 != null) {
            newNode.next = addTwoNumbers(next1, next2, newNode);
        } else if (newNode.val >= 10) {
            newNode.next = addTwoNumbers(next1, next2, newNode);
        }
        return newNode;
    }
}

这里递归的思路其实跟我们之前的遍历是一样的,这样的递归我们也称为尾递归,尾递归通常都可以通过使用循环提代。不建议使用这种写出,容易出现StackOverFlow

相关文章
|
21天前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
1月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
1月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3
|
1月前
|
容器
《LeetCode》——LeetCode刷题日记1
《LeetCode》——LeetCode刷题日记1
|
1月前
|
算法
LeetCode刷题---21.合并两个有序链表(双指针)
LeetCode刷题---21.合并两个有序链表(双指针)
|
1月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
|
1月前
|
存储 算法
LeetCode刷题---209. 长度最小的子数组(双指针-滑动窗口)
LeetCode刷题---209. 长度最小的子数组(双指针-滑动窗口)
|
1月前
|
算法 测试技术
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
|
1月前
|
算法 安全 数据处理
LeetCode刷题---707. 设计链表(双向链表-带头尾双结点)
LeetCode刷题---707. 设计链表(双向链表-带头尾双结点)
|
1月前
|
存储
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
140 38