【LeetCode】解题 2:Add Two Numbers

Leetcode解题之路(golang版):2. 两数相加(Add Two Numbers) 2. 两数相加 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> ... 阅读详情

Problem 2: Add Two Numbers [Medium]

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.

Example:

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

来源:LeetCode

Solution (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 head = null;
        ListNode tail = null;
        for(; l1 != null && l2 != null; l1 = l1.next, l2 = l2.next){
            ListNode l3 = new ListNode(l1.val + l2.val);
            if(head == null){
                head = l3;
            }
            else{
                if(tail.val >= 10){
                    tail.val -= 10;
                    l3.val += 1;
                }
                tail.next = l3;
            }
            tail = l3;
        }
        while(l1 != null){
            ListNode l3 = new ListNode(l1.val);
            if(tail.val >= 10){
                tail.val -= 10;
                l3.val += 1;
            }
            tail.next = l3;
            tail = l3;
            l1 = l1.next;
        }
        while(l2 != null){
            ListNode l3 = new ListNode(l2.val);
            if(tail.val >= 10){
                tail.val -= 10;
                l3.val += 1;
            }
            tail.next = l3;
            tail = l3;
            l2 = l2.next;
        }
        if(l1 == null && l2 == null && tail.val >= 10){
            ListNode l3 = new ListNode(1);
            tail.val -= 10;
            tail.next = l3;
        }
        return head;
    }
}

修改过程

  • while循环体内忘记写tail.next = l3,导致输入[1,8] [0],输出错误答案为[1]。
  • 末尾进位的if判断没有写在最后,没有考虑两数长度不一致并且最后需要进位的情况,导致输入[1] [9,9],输出错误答案为[0,10]。
【力扣Leetcode解题系列之0002. 两数之和Add Two Numbers:链表与数学运算的完美结合】 力扣2题"Add Two Numbers"是一道经典的链表加法问题,要求将两个逆序存储数字的链表相加并返回结果链表。解题关键在于模拟十进制加法过程,处理不同长度链表和进位问题。主要有两种解法:循环法和递归法。循环法通过遍历链表逐位相加,处理进位;递归法则通过递归调用处理后续节点和进位。两种方法的时间复杂度均为O(max(M,N)),空间复杂度为O(1)。题目考察了链表操作和数学运算的结合能力,是算法练习的典型范例。 阅读详情

相关推荐

python算法之旅(2)- Add Two Numbers

# 转载请注明出处 http://blog.youkuaiyun.com/qq_34175893/article/details/79636790# 打算开始用python学习算法,并进行一系列的学习过程及心得体会的记录,欢迎大家持续关注,一起学习。欢迎大家提出意见或建议# 不关心问题的解决,只关心不同的解决的问题的思路,所有的思路均在代码中注释,大家边看代码边看思路,# _*_ coding:utf-8 _...

qq_34175893的博客 4048

LeetCode 2:Add Two Numbers解题

终于,隔了很久之后,我又开始题了。 其实刷OJ是为了保研机试提前做准备,也是为了给自己的编程知识保温,但是当我发现自己两个月了还在刷easy难度的题目的时候,我知道,我得挑战点真正的题目了。 还是先来看一下今天的题目 You are given two non-empty linked lists representing two non-negative integers. The d

GhostLWB的博客 335

LeetCode --- 2. Add Two Numbers 解题报告

2. Add Two Numbers 解题报告

杨鑫newlife的专栏 513

LeetCode: 2. Add Two Numbers 【C 解题】

2. Add Two Numbers 【C 解题】关键字:Linked List, Math 题目要求: Medium You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes co

u010897682的专栏 512

PROFINET方案选型指南:瑞萨/西门子/开源p-net协议栈对比实测

本文基于瑞萨RZ/N2L硬件平台,对瑞萨官方认证协议栈、西门子ERTECH及开源p-net三种PROFINET方案进行了实测对比。重点分析了PROFINET-IRT实时性、CPU/内存资源消耗、开发集成复杂度及许可证成本等核心指标,为工业设备制造商提供了从性能到商业决策的全面选型指南。

weixin_29291185的博客 208

从玩具到工业:手把手教你用Webots仿真Scara抓取(逆解实战篇)

本文详细介绍了如何在Webots仿真环境中实现Scara机械臂的逆解计算与抓取操作,涵盖从环境配置、逆运动学算法到轨迹规划与实时控制的完整流程。通过实战案例演示,帮助读者掌握机械臂逆解的核心技术,并应用于工业自动化场景,提升Scara机械臂的编程与控制能力。

weixin_30932215的博客 504

[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 two numbers and return it as a lin

云先生的博客 401

论文阅读—Square Attack: a query-efficient black-box adversarial attack via random search

论文阅读—Square Attack: a query-efficient black-box adversarial attack via random search 发表位置 ECCV 2020 摘要 作者提出了一种基于得分的黑盒Square attack,该模型不依赖于模型的局部梯度信息,因此可以绕过梯度隐藏防御攻击。Square Attack是一种随机搜索方法,它在随机位置选择局部的方形更新,使得每次迭代时扰动近似位于可行集的边界。 背景 当涉及到对安全至关重要的机器学习应用时,对抗性的例子尤其值得

yayayamaomaoya的博客 2492

LeetCode //C - 445. Add Two Numbers II

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

Made in Code 1897

从充电管理芯片里‘偷师’:拆解一个PMOS双向开关电路,搞定你的固态继电器需求

本文深入解析了PMOS双向开关电路在固态继电器应用中的设计要点,重点探讨了防电流倒灌和高侧双向开关的实现技巧。通过拆解充电管理芯片中的PMOS背靠背结构,工程师可以构建高效可靠的固态开关方案,适用于智能硬件和电池管理系统。文章还提供了关键参数选择、电路优化建议及实测数据对比,助力解决电源防反接等工程难题。

weixin_33698043的博客 481

51单片机基础知识

前言 周末闲着无聊,以后定时更新一期关于51单片机的学习案例,以供大家平时参考使用。至于单片机,你可以理解为人的大脑,而人各项身体器官的运作需要意识给到大脑,然后大脑再驱动我们的中枢神经进行相应的动作,这里的意识可以理解为我们要写的程序,而人的器官相当于各种输出设备,例如我们以后要学的LED、电机、各种传感器等等。学好单片机能让你找到一份相对不错的工作,当然越可观的东西也是要付出相应努力的。 一、51单片机是什么? 通常我们一说到 51 单片机,指的都是兼容IntelMCS-51体系架构的一系列单片机,而

qq_43537819的博客 6903

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

浅析MOS管导通过程及RC吸收电路设计要点

RC吸收电路 米勒电容 MOS导通过程

liangbo1999的博客 2万+

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

LLVM 编译器学习笔记之三十六-- 指令调度Instruction scheduling

1、参考Instruction scheduling in LLVM - 知乎,在中后端均存在指令调度 指令调度模型 2、在llvm12->llvm14 中Machine Instruction Scheduler有个patch增强了load/store指令的合并,也就是指令调度不仅仅只是改变指令的顺序,该优化类似gcc中的store-merge优化 .....................................................................

~ 飞 扬 的 天 空 ~ 5686

【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

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode解题笔记 - 2. Add Two Numbers

2. Add Two Numbers You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the...

a541967687的博客 209

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

NTscan扫描工具

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

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

转载请注明出处:http://blog.youkuaiyun.com/crazy1235/article/details/51820937Subject 出处:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-negative numbers. The dig

crazy_jack 1万+

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

闯关leetcode——2.Add Two Numbers

leetcode第二题

方亮的专栏 1120

Add Two Numbers

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

Yuke的博客 238

[leetcode] 2. Add Two Numbers @ python

文章目录原题解题思路代码 原题 https://leetcode.com/problems/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...

闲庭信步 754

Leetcode1-50: 02, Add two numbers

Leetcode1-50: 02, 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

Y123iwantit的博客 213

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

Add Two Numbers -- LeetCode

原题链接: http://oj.leetcode.com/problems/add-two-numbers/  这道题比较简单,是cc150里面的题,思路很明确,就是按照位数读下去,维护当前位和进位,时间复杂度是O(n),空间复杂度是O(1).代码如下:  public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int car

Code Ganker 1万+

LeetCode刷题笔记(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...

Vensmallzeng的博客 399
下一篇: 【LeetCode】解题1:Two Sum
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值