LeetCode 2《Add Two Numbers问题》

[LeetCode]--2. Add Two Numbers You are given two linked lists representing two non-negative numbers. 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 linke 阅读详情

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.

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

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

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

Example:

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

解法:

对于这种高精度加法问题,我们首先想到的便是Dummy Head,Dummy Head是一种非常有用的技巧,容易写出bug free的code。
有了dummy之后,所有的节点都变成拥有前置节点的节点了。
所以就不用担心处理头节点这个特殊情况了。
两种特殊情况需要注意:
1.不同位数
2.答案的位数增多
这里提供一种通用的高精度处理思路
(伪代码)

dummy=tail=ListCode(0)
while(L1 or L2 or carry)
{
sum=L1.val+L2.val+carry
tail.next=ListCode(sum%10)
tail=tail.next
carry=sum/=10
L1=L1.next
L2=L2.next
}
return dummy.next

Time complexity: o(max(n,m))
Space complexity: o(max(n,m))

java代码:

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
                ListNode tail = new ListNode(0);
	    	    ListNode dummy = tail;
	    	    int sum = 0;
	    	    while (l1 != null || l2 != null || sum > 0) {
	    	      sum += (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val);
	    	      tail.next = new ListNode(sum % 10);
	    	      tail = tail.next;
	    	      if (l1 != null) l1 = l1.next;
	    	      if (l2 != null) l2 = l2.next;
	    	      sum /= 10;
	    	    }
	    	    return dummy.next;
    }
}
LeetCode --- 2. Add Two Numbers 解题报告 2. Add Two Numbers 解题报告 阅读详情

相关推荐

LeetCode之Add Two Numbers

LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the tw

码莎拉蒂 2033

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 i

gddxmmxf的博客 353

无线控制的隐形桥梁:深入解析NRF24L01在电机控制中的数据传输艺术

本文深入解析NRF24L01无线模块在电机控制中的数据传输技术,涵盖通信机制优化、抗干扰策略及实时性保障。通过舵机和无刷电机的实战案例,展示如何构建稳定高效的无线控制系统,为嵌入式开发提供完整解决方案。

j7k8l的博客 333

LeetCode 2. Add Two Numbers - 链表(Linked List)系列题1

题目大意:用两个非空链表来表示两个非负整数,链表的一个节点表示数字的其中一位,数字在链表中是以反顺序存储的,即最低位在前最高位在后。要求相加两个数把和存到一个新链表中。

hgq522的博客 380

拆解爆火AI图文日报!用扣子(Coze)工作流,10分钟实现自动化新闻早报

当用户提供一则新闻内容时,仔细分析新闻中的关键信息。提取新闻事件的核心要点,包括事件主体、发生时间、地点、主要情节等。用简洁的语言对新闻进行总结,控制在[X]字以内(可根据实际需求设定字数限制)。若用户提供多条新闻,对比各新闻之间的异同点。从事件类型、影响范围、发展趋势等方面进行分析。综合总结多条新闻的整体情况,以有条理的方式呈现给用户。

一身都是月 2430

手把手带你搞懂电压跌落测试DIP

砖一用4分钟时间手把手带你搞懂电压跌落测试DIP,希望对你有用~

qq_45508321的博客 7925

LeetCode 2. Add Two Numbers(C++)

使用了O(1)的额外空间,思路是将两个链表遍历并合并,合并过程中不断计算进位,如果计算到最后一次加法仍有进位(可能是两个链表对应位置值相加产生进位或者上一次的进位加本次值又产生进位),那么需要加一个值为1的新结点。 这种思路的优点是空间复杂度小,缺点是实现比较复杂(跟借助一个额外的数组和链表相比)。 /** * Definition for singly-linked list. * st

赞美月亮的专栏 1436

Deepseek 10分钟化身理性荐股神器:触碰程序员不敢想的天花板

在这个消息疯狂、波动频繁的市场,“理性”两字显得弥足珍贵。通过Deepseek+AkShare+Aktools+Dify四位一体的方案,能帮我们降低信息搜集成本,并在短期投资领域搭建出一套“可解释、低门槛、易定制”的量化荐股流程。不要再盲目听小道消息或依赖情绪驱动,不妨给自己一个机会,让数据说话。而要注意的是,“理性荐股”并非万能,它只是工具,最终的收益还得依赖投资者自身对风险和机遇的权衡。祝大家都能在这股市里收获理性的底气,寻得更精准的入场时机。

qqxdh的博客 3755

[leetcode] 2. Add Two Numbers

You are given two linked lists representing two non-negative numbers. 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 link

TstsUgeg的博客 407

【自用】吉林大学计算机系统结构第二章:缓存优化 曲冠南老师版 笔记

容量8KB,2的十三次方,块大小16B,一块用掉四位,剩余2的九次方是标志位,则有512行,每个元素8B,则一块两个元素,a数组150块,b数组152块,不出现容量失效。不管是否交换内容,在同一“伪相联”组中的两块都是用同一个索引得到的,因此失效率相同,即:失效率伪相联=失效率2路。伪相联cache的命中时间等于直接映象cache的命中时间加上伪相联查找过程中的命中时间*该命中所需的额外开销。(6) 由编译器控制的预取,硬件预取的替代方法,在编译时加入预取的指令,在数据被用到之前发出预取请求。

weixin_73587775的博客 1617

ArcGIS 10.4 Desktop安装步骤。

步骤 1 :安装 LicenseManager 有了安装包以后,我们进入安装包文件目录,如图所示,首先需要安装ArcGIS license manager,即点击打开图中红色框中exe文件。 PS:安装 LicenseManager,关于 License 的安装路径一般默认即可,因为很小的,装C 盘也无所谓了,当然了自己改了也可以的,但是不建议! 即便修改路径,建议只改盘符,不修改相对路径方式! 如:C:\Program Files (x86)\ArcGIS\License10.6 修改为 D:\.

IT爱好者 2万+

LeetCode 2.Add Two Numbers 两数相加 Python3 三种解法

LeetCode 2.Add Two Numbers 两数相加 Python3 由于做提前连链表是怎么实现的都不知道所以补了点课。 Single Linked List 代码来自这位兄台 pasilo https://blog.51cto.com/13047263/2070815 内容理解我看的知乎 如何更好的理解链表 以下代码和LeetCode上面的链表稍微有些许不同,那个里面链表和链表上面的节...

Aixoum is Me 1290

【DaVinci Configurator专题】-5-通过Data Mapping自动创建CAN信号关联的Port并添加到APP SWC

目录 1 导入CAN Matrix变更后的Arxml文件 2 自动创建CAN信号关联的APP Port 3 CAN信号与App SWC的接口映射/连接 4 结尾 <--返回「实战篇」博文-总目录--> <--返回「Autosar从入门到精通-实战篇」专栏主页--> 关键词: DaVinci Developer DaVinci Configurator Pro ARXML AUTOSAR System Description Network Explorer 1

「汽车电子助手」的博客 5130

LeetCode——Add Two Numbers

LeetCode——Add Two Numbers leetcode上第二题: 题目内容: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a

cassiePython的专栏 624

【LSTM回归预测】attention机制LSTM时间序列回归预测【含Matlab源码 1992期】

attention机制LSTM时间序列回归预测 完整的代码,方可运行;可提供运行操作视频!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 2592

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Leetcode 2 Add Two Numbers

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 t...

博学 笃志 切问 近思 197

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

LeetCode-2. Add Two Numbers

刚刚我经历了可能是2017年3月22号的最大的大起大落。

public Blog myBlog(){ 1127

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

[LeetCode] 2. Add Two Numbers

2. Add Two Numbers 反序求和 链表操作

ZY的博客 302

LeetCode 445. Add Two Numbers II

问题描述 You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and r...

Keep Learning 211

[LeetCode]2. Add Two Numbers用链表逆序存储的两个数相加

我第一反应的一个想法是:先分别遍历两个链表,求得两个加数add1和add2,然后求得sum=add1+add2,最后再将sum用链表按位逆序存储。这种方法忽略了链表长度很大时数据并不能存储在int或其他整型类型里的问题。即此题应该注意是一个大数问题。 class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListN...

xiaoxiaosuwy的博客 242

LeetCode //C - 445. Add Two Numbers II

【代码】LeetCode //C - 445. Add Two Numbers II。

Made in Code 1897

Leetcode[2] Add Two Numbers and Leetcode[445] Add Two Numbers II

Leetcode[2] Add Two Numbers class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ ...

只为分享 252

JS斩杀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 a

清风的博客 2225

leetcode Add Two Numbers

最近一直在忙实验室项目的事情,好久没写代码了,手太生了,所以去leetcode官网随便找了两个简单题刷了一下,第一个一遍就通过了,第二个还是有点问题。感觉这种东西就得多练。不然会一直退步退步,下面是我写的两个题的解法,仅供参考:(先写一题吧下一题之后补上) Add Two  Numbers //C/C++解法 /**  * Definition for singly-linked

yaojianpengshr的博客 659

2. Add Two Numbers

2. Add Two Numbers Medium 59971568Share 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...

Joy 651

【LeetCode】2. Add Two Numbers 解题报告(Python)

【LeetCode】2. Add Two Numbers 解题报告(Python)题目描述解法一:先求和,再构建链表解法二:采用一个进位carry方便的完成一次遍历得出结果解法三:递归 题目地址:https://leetcode.com/problems/add-two-numbers/ 题目描述 You are given two non-empty linked lists represent...

ttinch的博客 261
上一篇: LeetCode 1《A + B 问题》
下一篇: 浅谈剪枝
Andy in boots
博客等级 码龄7年 73粉丝 69原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值