LeetCode:Add Two Numbers

【力扣Leetcode解题系列之0002. 两数之和Add Two Numbers:链表与数学运算的完美结合】 力扣2题"Add Two Numbers"是一道经典的链表加法问题,要求将两个逆序存储数字的链表相加并返回结果链表。解题关键在于模拟十进制加法过程,处理不同长度链表和进位问题。主要有两种解法:循环法和递归法。循环法通过遍历链表逐位相加,处理进位;递归法则通过递归调用处理后续节点和进位。两种方法的时间复杂度均为O(max(M,N)),空间复杂度为O(1)。题目考察了链表操作和数学运算的结合能力,是算法练习的典型范例。 阅读详情

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.

public static ListNode AddTwoNumbers1(ListNode l1, ListNode l2)
            {
                ListNode l1Current=l1;
                ListNode l2Current=l2;
                ListNode resultListCurrent = null;
                ListNode resultListHead= null;
                bool isCarry = false;
                do
                {
                    int result= (l1Current!=null?l1Current.val:0) + (l2Current != null ?  l2Current.val:0)+(isCarry?1:0);
                    isCarry = false;
                    if (result > 9) {
                        result =  Convert.ToInt32(result.ToString().Substring(result.ToString().Length - 1));
                        isCarry = true;
                    }
                    ListNode resultNode= new ListNode(result) { next = null };
                    if (resultListCurrent != null)
                        resultListCurrent.next = resultNode;
                    else
                        resultListHead = resultNode;
                    resultListCurrent = resultNode;
                    l1Current = l1Current != null ? l1Current.next : null ;
                    l2Current = l2Current != null ? l2Current.next : null;
                } while (!(l1Current == null &&l2Current == null&& !isCarry));
                return resultListHead;
            }
        public static ListNode AddTwoNumbers2(ListNode l1, ListNode l2)
        {
            ListNode l1Current = l1;
            ListNode l2Current = l2;
            ListNode resultListCurrent = null;
            ListNode resultListHead = null;
            bool isCarry = false;
            do
            {
                int result = (l1Current != null ? l1Current.val : 0) + (l2Current !=  null ? l2Current.val : 0) + (isCarry ? 1 : 0);
                isCarry = result/10 >0? true:false ;
                
                ListNode resultNode = new ListNode(result%10) { next = null };
                if (resultListCurrent != null)
                    resultListCurrent.next = resultNode;
                else
                    resultListHead = resultNode;
                resultListCurrent = resultNode;
                l1Current = l1Current != null ? l1Current.next : null;
                l2Current = l2Current != null ? l2Current.next : null;
            } while (!(l1Current == null && l2Current == null && !isCarry));
            if (isCarry)
                resultListCurrent.next = new ListNode(1);
            return resultListHead;
        }
Python之ae-toolbox包语法、参数和实际应用案例 ae-toolbox(全称 after-effects-toolbox)是 Python 生态中专门用于 Adobe After Effects(AE)自动化操作 的工具包,基于 AE 的 ExtendScript(JSX)桥接技术,实现 Python 与 AE 的跨语言通信。 阅读详情

相关推荐

C# NX二次开发:两种过滤器设置方案的实战解析与场景适配

本文深入解析C# NX二次开发中的两种过滤器设置方案:AddFilterHandler回调与MaskTriple属性设置。通过实战案例对比其性能差异与适用场景,帮助开发者根据需求选择最优方案,提升NX插件开发效率与交互体验。特别针对BlockStyler集成场景提供专业建议,是NX二次开发者的实用指南。

weixin_33739646的博客 844

【Java版】LeetCode 力扣第 2 题:两数相加 (Add Two Numbers)

题目描述 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 You are given two non-empty l

代码时光 752

微处理器实验报告-实验四

实验四 可编程定时/计数器8253实验 一、实验要求 利用8086外接8253可编程定时/计数器,可以实现方波的产生。 二、实验目的 1、学习8086与8253的连接方法。 2、学习8253的控制方法。 3、掌握8253定时器/计数器的工作方式和编程原理

青竹小轩的博客 5112

LeetCode:Add Two Numbers

Add Two Numbers - LeetCode

杂的文 639

Xilinx FPGA MultiBoot实战:7Series与UltraScale的SPI Fallback配置详解

本文深入解析了Xilinx FPGA的MultiBoot(多启动)与Fallback(回退)功能,针对7系列与UltraScale/UltraScale+系列,提供了从约束文件编写、看门狗集成到镜像烧录的完整实战配置指南。文章重点对比了两个系列在SPI Fallback配置上的关键差异与注意事项,旨在帮助工程师构建高可靠、永不宕机的FPGA系统。

weixin_29197051的博客 547

Abaqus中Cohesive单元建模:从本构到失效的完整指南

本文提供了Abaqus中Cohesive单元建模的完整指南,涵盖从本构模型到失效分析的全过程。详细解析了Cohesive单元作为模拟界面粘结与失效的关键工具,其核心在于弹性本构、损伤起始准则和损伤演化定律的设置。文章通过双悬臂梁(DCB)实例,手把手指导前处理、材料定义、求解设置及后处理分析,并分享了刚度、强度、断裂能等关键参数的影响与收敛性调优的实用避坑技巧,帮助用户准确模拟复合材料及胶接结构的界面失效行为。

cuda7parallel的博客 700

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

基于Arduino与Velostat的智能鞋垫:DIY足底压力监测系统全解析

压力传感器是物联网和健康监测领域的基础元件,其核心原理是将物理压力转换为可测量的电信号。通过压阻效应,材料的电阻值随压力变化,从而实现压力感知。这项技术为低成本、定制化的健康监测方案提供了可能,尤其在运动分析和康复训练中具有重要价值。本文聚焦于利用Velostat压敏材料和Arduino平台,详细解析如何从传感器制作、信号调理到无线数据传输,构建一套完整的足底压力监测系统。文中深入探讨了传感器阵列构建、信号滤波等关键技术,并针对常见问题提供了实用的解决方案,为创客和开发者实现可穿戴健康监测设备提供了清晰的工

weixin_32818781的博客 402

LeetCode题解:Add Two Numbers

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 an

Magi Su的技术回收站 5462

语义分割系列7-Attention Unet(pytorch实现)

本文介绍了AttentionUnet模型和其主要中心思想,并在pytorch框架上构建了Attention Unet模型,构建了Attention gate模块,在数据集Camvid上进行复现。

yumaomi的博客 2万+

新手必看!华为S5700交换机健康检查清单:从CPU到日志的完整巡检流程

本文为新手网管人员提供华为S5700交换机的健康检查完整指南,涵盖CPU、内存、温度、日志等核心指标的日常维护流程。通过详细的display命令和实用案例,帮助用户快速掌握设备体检技巧,确保交换机稳定运行。

weixin_29251337的博客 237

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

UG二次开发GRIP刻字

UG二次开发GRIP刻字

qq285503851的博客 1138

[LeetCode] 005: Add Two Numbers

[Problem]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 i

1.02^365的成长裂变 3636

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode: Add Two Numbers

LeetCode: Add Two NumbersYou 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

EmilSinclair4391的博客 777

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Leetcode2: Add Two Numbers

这道题的难度是简单,说也简单,但也不简单 说不简单是因为,我为了这个审题的事儿,磨蹭了大半天 说简单是因为,审题审清楚了以后,写写也没多久 来看题目,他用一个链表的结构,来代表2个非负数,而非负数是链表中结点的数据元素,不过他是倒序的,也就是头结点是3和4 然后对应位数两两相加,输出一个也是链表类型的结果 接下来说觉得不简单的地方,因为,python里面是没有链表

bestallen的博客 857

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Leetcode: Add Two Numbers

题目连接:Add Two Numbers

no pains,no gains 567

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 nodes contain a single digit. Add the ...

时梦的博客 259

LeetCode算法题之2:Add Two Numbers

LeetCode之2:Add Two Numbers 问题描述: 将两个个位数数字组成的链表合并为一个,模拟加法器。 问题的陷阱: 在考虑进位问题时想的不全名,漏掉两个链表最终同时为空,而产生进位,导致最终少加一位数这一问题。 缺陷代码分析 /** * Definition for singly-linked list. * public class ListNode { * in...

veatheroe的博客 235

LeetCode 2: Add Two Numbers题解(Python)

Leetcode 2: Add Two Numbers 分类:Linked List 难度:Midium (M-) 描述: 给两个链表(linked list),链表元素是int形式,求两个链表代表的数的和 Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807. 链接: Add Two Numbers 思路: 此题稍微麻烦之处在于要用linked lis

Bourns_A的博客 424

【LeetCode】解题 2:Add Two Numbers

LeetCode解题思路 2:Add Two NumbersProblem 2: Add Two Numbers [medium]Solution修改过程 Problem 2: Add Two Numbers [medium] You are given two non-empty linked lists representing two non-negative integers. The d...

Fayedily的博客 205

Leetcode: Add Two Numbers

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

spirit_young的博客 426
上一篇: Leetcode:Two Sum
下一篇: A Brief History of Object-Oriented Programming: 面向对象简史
Ken_Li_Lsq
博客等级 码龄14年 22粉丝 61原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值