2_两数相加

目录

  • 2_两数相加

    • 描述
    • 方法一:小学数学
      • 思路
      • Java 代码(非递归写法)
      • Java 代码(递归写法)
      • Python 代码(非递归写法)

2_两数相加

描述

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

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

示例:

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

方法一:小学数学

思路

按照小学数学中求两数之和的做法,从最低位(链表表头)开始加起,用变量 carry 保存进位的结果(初始值为0),每次求和之后更新变量 carry 的值。

Java 代码(非递归写法)

/**
 * 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) {
        ListNode dummyHead = new ListNode(-1);
        ListNode curNode = dummyHead, p = l1, q = l2;
        int carry = 0;

        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = x + y + carry;
            carry = sum / 10;
            curNode.next = new ListNode(sum % 10);
            curNode = curNode.next;

            if (p != null) {
                p = p.next;
            }
            if (q != null) {
                q = q.next;
            }
        }

        if (carry > 0) {
            curNode.next = new ListNode(carry);
        }

        return dummyHead.next;
    }
}
// Runtime: 38 ms
// Your runtime beats 46.06 % of java submissions.

复杂度分析:

  • 时间复杂度:\(O(max(m, n))\),其中 \(m\) 和 \(n\) 分别表示两个链表的长度。
  • 空间复杂度:\(O(max(m, n))\),返回链表的长度最多为 \(max(m, n) + 1\)。

Java 代码(递归写法)

/**
 * 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) {
        return addTwoNumbers(l1, l2, 0);
    }

    private ListNode addTwoNumbers(ListNode l1, ListNode l2, int carry) {
        // Recursive termination condition
        if (l1 == null && l2 == null) {
            return carry > 0 ? new ListNode(carry) : null;
        }

        int sum = carry;
        ListNode l1Next = null, l2Next = null;
        if (l1 != null) {
            sum += l1.val;
            l1Next = l1.next;
        }
        if (l2 != null) {
            sum += l2.val;
            l2Next = l2.next;
        }
        ListNode curr = new ListNode(sum % 10);
        curr.next = addTwoNumbers(l1Next, l2Next, sum / 10);
        return curr;
    }
}
// Runtime: 27 ms
// Your runtime beats 94.02 % of java submissions.

复杂度分析同上。

Python 代码(非递归写法)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        dummy_head = ListNode(-1)
        cur = dummy_head
        carry = 0

        while l1 or l2 or carry:
            v1 = v2 = 0
            if l1:
                v1 = l1.val
                l1 = l1.next
            if l2:
                v2 = l2.val
                l2 = l2.next
            carry, val = divmod(v1 + v2 + carry, 10)
            cur.next = ListNode(val)
            cur = cur.next
        return dummy_head.next

# Runtime: 156 ms
# Your runtime beats 43.08 % of python3 submissions.

复杂度分析同上。

原文地址:https://www.cnblogs.com/xugenpeng/p/9772935.html

时间: 2024-08-15 07:14:21

2_两数相加的相关文章

[CareerCup] 18.1 Add Two Numbers 两数相加

18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我们实现两数相加,但是不能用加号或者其他什么数学运算符号,那么我们只能回归计算机运算的本质,位操作Bit Manipulation,我们在做加法运算的时候,每位相加之后可能会有进位Carry产生,然后在下一位计算时需要加上进位一起运算,那么我们能不能将两部分拆开呢,我们来看一个例子759+674 1.

程序猿之---C语言细节20(符号和有符号之间转换、两数相加溢出后数值计算)

主要内容:无符号和有符号之间转换.两数相加溢出后数值计算 #include <stdio.h> /* 这个函数存在潜在漏洞 */ float sum_elements(float a[], unsigned length) { int i; float result = 0; for(i = 0; i <= length - 1; i++) { result += a[i]; printf("a[%d] = %f \n",i,a[i]); } return resul

【LeetCode】- Two Sum(两数相加)

[ 问题: ] Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

程序员之---C语言细节20(符号和有符号之间转换、两数相加溢出后数值计算)

主要内容:无符号和有符号之间转换.两数相加溢出后数值计算 #include <stdio.h> /* 这个函数存在潜在漏洞 */ float sum_elements(float a[], unsigned length) { int i; float result = 0; for(i = 0; i <= length - 1; i++) { result += a[i]; printf("a[%d] = %f \n",i,a[i]); } return resul

leetcode 2. 两数相加

给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val

【leetcode】 算法题2 两数相加

问题 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 代码实现 #include <vector> #include <map> #include <iostream>

链表表示的两数相加

给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 代码一: 1 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 2 ListNode res

leetcode刷题2:两数相加add_two_numbers

题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 思路: 本题的思路很简单,按照小学数学中学习的加法原理从末尾到首位,对每一位对齐相加即可. 技巧在于如何处理不同长度的数字

C语言实现两数相加2018-09-23

/*给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807*//** * Definition for singly-linked list. * struct ListNode { * int val; * str