LeetCode 2:两数相加 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.

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

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

示例:

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

解题思路:

将两个链表遍历将相同位置节点值累加,其和过十进一,新链表相对位置节点值取其和的个位数值。需要考虑到两个链表长度不同时遍历方式、链表遍历完成时最后一位是否需要进一位。

Java:

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);//虚拟头节点
        ListNode cur = head;//指针
        int carry = 0;//进位值
        while (l1 != null || l2 != null) {//两个链表均为空时停止遍历
            int x = (l1 != null) ? l1.val : 0;//x为l1的值,如果节点为空,值为0
            int y = (l2 != null) ? l2.val : 0;//y为l2的值,如果节点为空,值为0
            int sum = carry + x + y;//sum为两节点值之和
            carry = sum / 10;//得进位值(1)
            cur.next = new ListNode(sum % 10);//sum%10 得余数即 个位数的值
            cur = cur.next;//刷新指针
            if (l1 != null) l1 = l1.next;//l1节点不为空继续刷新下一个节点
            if (l2 != null) l2 = l2.next;//l2节点不为空继续刷新下一个节点
        }
        if (carry > 0) {//如果仍然需要进 1 ,则直接新建一个节点
            cur.next = new ListNode(carry);
        }
        return head.next;
    }
}

Python3:

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        head = ListNode(0)
        cur = head
        sum = 0
        while l1 or l2:
            if l1:#l1不为空
                sum += l1.val#累计两节点值的和
                l1 = l1.next#刷新节点
            if l2:
                sum += l2.val#累计两节点值的和
                l2 = l2.next#刷新节点
            cur.next = ListNode(sum % 10)//刷新新链表
            cur = cur.next
            sum = sum // 10
        if sum != 0:
            cur.next = ListNode(sum)
        return head.next

欢迎关注公.众号一起刷题: 爱写Bug

目录
相关文章
|
5月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
25 0
|
7月前
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
10月前
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
65 0
LeetCode 415. Add Strings
LeetCode 357. Count Numbers with Unique Digits
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n 。
61 0
LeetCode 357. Count Numbers with Unique Digits
|
存储 Python
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
73 0
LeetCode 315. Count of Smaller Numbers After Self
LeetCode 258. Add Digits
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
51 0
LeetCode 258. Add Digits