2. Add Two Numbers

LeetCode --- 2. Add Two Numbers 解题报告 2. Add Two Numbers 解题报告 阅读详情

给定两个非空链表代表两个非负整数,每位数存储在一个倒置的链表中结点,将这两个数相加,返回结果链表.

你可以假设这两个数字不包含任何前导零,除了数字0本身。

Example

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

思路:

可以看到链表翻转过来的数字相加如下。而题目中需要输出的链表为结果的反转708。

进位是由低位向高位进的,那么对于反转的数来说就是高位向低位进位才能取得反转结果,如下面两个链表所示,从左到右遍历链表,两数相加进位。

需要注意的问题为:

(1)进位

(2)有空链表

(3)两个链表长度不一样

代码如下(官方答案):

此处设了一个虚拟节点,这是为了让头结点的新建赋值过程与其他结点保持一致,否则需另外写语句为头结点赋值。

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;
}

[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 阅读详情

相关推荐

Python | KS检验以及其余非参数检验的实现

Python | KS检验以及其余非参数检验的实现1 什么是KS检验2 KS检验分类?3 KS检验的Python实现3.1 检验指定的数列是否服从正态分布3.2 检验指定的两个数列是否服从相同分布4 其余的非参数检验4.1 Wilcoxon符号秩检验(t检验的非参数版本)4.2 Kruskal-Wallis H检验(方差分析的非参数版本)4.3 Mann-Whitney秩检验5 参考 1 什么是KS检验 定义:检验一个分布f(x)与理论分布g(x)【比如正态分布】是否一致,或两个观测值分布是否有显著差异的检

RUC_Lee的博客 1万+

LeetCode 2. Add Two Numbers(C++)

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

赞美月亮的专栏 1435

2026 具身智能 VLA/WAM 全栈学习与训练路线:从动作建模、SFT、RL 到世界模型的 RTX PRO 6000 96GB 单卡实战指南

我会把这张表做成“”而不是只列论文名:先核对每个模型截至的公开代码、训练目标和数据接口,再单独给你一套。对于没有公开、只有推理代码或训练细节不完整的模型,我会明确标出来,避免把“论文里提到”误写成“可复现”。目前有一个重要更新:。截至今天 NVIDIA 官方开源主线已经是换成 Cosmos-Reason2-2B/Qwen3-VL 架构,动作端仍是连续路线,而且官方明确给出单卡微调时默认只训练,峰值显存约。所以你的 96GB 单卡对 GR00T SFT 非常宽裕。可以。下面我按,把它整理成一张真正面向“

u013250861的博客 446

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

车载面试题目(ADAS)

多数功能都是依靠的前置传感器(经过Lidar、Radar、Camera融合),比如ACC、AEB、LKA、NOA、TJA、FCW等,只有少数涉及周视传感器,比如BSD、CVW、DOW。③如果前面有车切速度小于自车车速,自车减速对减速度有要求,比如3.5m/m2,④如果前车静止了,自车有个跟停距离要求,比如2-3m。试验场有不同曲率的弯道,比如125m、250m、500m的,根据需求跟车行驶,看是否会减速,能不能驶出弯道等。迭代内容:底层通讯、网络管理、诊断、刷写、信息安全、OTA。

qq_43475097的博客 2298

用矿卡P104-100在PVE 8.0.4上跑Ollama:保姆级显卡直通与驱动安装避坑指南

本文详细介绍了如何在PVE 8.0.4环境下通过显卡直通技术,利用矿卡P104-100高效部署本地大模型Ollama的全流程。从硬件配置、BIOS设置到驱动安装和性能调优,提供了保姆级的避坑指南和实战解决方案,特别针对矿卡无显示输出的特殊性进行了优化。

weixin_33228769的博客 752

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

网工实验笔记:SNMP的配置

简单网络管理协议SNMP(Simple Network Management Protocol)是广泛应用于TCP/IP网络的网络管理标准协议。SNMP提供了一种通过运行网络管理软件的中心计算机(即网络管理工作站)来管理设备的方法。

2301_76170756的博客 2116

leetcode 21. Merge Two Sorted Lists和2. Add Two Numbers

【代码】leetcode 21. Merge Two Sorted Lists和2. Add Two Numbers。

weixin_41554427的博客 442

ANSYS Fluent热边界条件避坑指南:为什么你的自然对流计算结果总出错?

本文深入剖析了在ANSYS Fluent中进行自然对流仿真时,导致计算结果出错或发散的核心原因与解决方案。重点解析了重力项与操作密度设置、Boussinesq与不可压缩理想气体模型的抉择、以及压力离散格式(推荐Body Force Weighted)这三大关键雷区,并结合电子机箱散热等实战案例,提供了一套稳定可靠的仿真设置流程与调试技巧。

weixin_28396629的博客 741

(R,线性回归)R语言里的模型诊断图(Residuals vs Fitted,Normal QQ , Scale-Location ,Residuals Leverage)

线性回归,是概率统计学里最重要的统计方法,也是机器学习中一类非常重要的算法。线性模型简单理解非常容易,但是内涵是非常深奥的。尤其是线性回归模型中的Diagnostics plot的阅读与理解一直被认为是线性回归中的一个难点。 在任何线性模型中,能够直接“lm”(模型有意义),既要考虑各个参数的t-test所得出的p-value,也要考虑总体模型F-检验得出的p-value。在这之后,还要清楚一个...

qq_35837578的博客 7万+

[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的博客 405

从FSI到三层堆叠:一文搞懂车载CIS图像传感器技术演进与选型避坑

本文深入解析车载CIS图像传感器从FSI到三层堆叠的技术演进,揭示不同结构在低光性能、HDR表现和温度稳定性上的关键差异。通过实测数据对比,指导智能驾驶系统选型策略,帮助开发者规避成本与性能的常见陷阱,提升车载摄像头在复杂环境下的成像质量。

weixin_30689307的博客 101

LeetCode-2. Add Two Numbers

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

public Blog myBlog(){ 1125

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

[LeetCode] 2. Add Two Numbers

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

ZY的博客 301

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

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

ccystewart的博客 167

【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

2 Add Two Numbers(两数相加)

LeetCode题库Add Two Numbers(两数相加)解答

iMotherBoard的博客 411

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

2.Add Two Numbers

题目描述:中等难度 You are given two non-empyt 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...

猫敷雪 239

[leetcode2] Add Two Numbers

[leetcode2]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

Cathy1994的专栏 464
上一篇: 1. Two Sum [SIMPLE]
下一篇: 使用文本编辑器编译python出现乱码问题
ack_Finding
博客等级 码龄10年 6粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值