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;
        }
【PDN仿真笔记9-使用Sigrity PowerDC进行IR Drop仿真的方法】 使用Sigrity PowerDC进行IR Drop进行仿真的方法 阅读详情

相关推荐

安装破解IAR EWARM 8.2,提供注册机

IAR Embedded Workbench for ARM(IAR for ARM)是一款微处理器的集成开发环境软件,该集成开发环境中包含了IAR的C/C++编译器、 汇编工具、链接器、库管理器、文本编辑器、工程管理器和C-SPY调试器,支持ARM,AVR,MSP430等芯片。   一、安装 双击运行安装程序,点击Install IAR Embedded Workbench for ARM...

睿思派克 4万+

【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

永磁同步电机矢量控制——电流内环前馈解耦策略

在采用永磁同步电机矢量控制时,ud和uq的指令值存在耦合项。需要对其进行解耦,目前,常用的解耦设计方法有前馈解耦、偏差解耦以及内模控制,本文主要分享电流内环前馈解耦控制策略。

a12312341ll的博客 8694

LeetCode:Add Two Numbers

Add Two Numbers - LeetCode

杂的文 639

Dofbot机械臂从零部署笔记(1)——ROS之创建URDF模型、配置Moveit和MotionPlanning

Dofbot机械臂从零部署笔记(1)——ROS之创建URDF模型、配置Moveit和MotionPlanning

takedachia的博客 8697

非root用户如何在CentOS7集群上编译安装OpenFOAM6

本文详细介绍了在CentOS7集群上以非root用户身份编译安装OpenFOAM6的完整流程。通过环境准备、依赖包离线获取、源码编译安装等步骤,解决了集群环境下权限和依赖问题,特别针对OpenMPI和Boost库提供了优化配置建议,帮助用户高效完成安装并优化性能。

weixin_28826961的博客 681

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

coze+飞书多维表格,批量分析小红书笔记,关键还能获取图片(附保姆级教程)

通过coze工作流,一键同步小红书账号和笔记链接到飞书多维表格,效率飞升~

qq_24403067的博客 4175

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

玩转 Milvus(二):在 Ubuntu 22.04(WSL2)上安装 Milvus

本文详细介绍了在Ubuntu 22.04(WSL2)环境下安装Milvus向量数据库的两种单机模式:Milvus Lite和Milvus Standalone。Milvus Lite作为轻量级Python库,适合快速学习和小规模实验,只需通过pip安装即可使用。Milvus Standalone则支持Docker和Docker Compose两种部署方式,适合开发测试或小规模生产环境。文章对比了不同安装方式的优缺点,并提供了验证案例和常见问题解答,帮助开发者快速上手,实现简单的语义搜索功能。

企业级后端与 AI 应用开发双线探索 2507

飞腾Ubantu22.04.3安装OpenNebula测试

因OpenneBula官方镜像源只有AMD架构的镜像包不存在ARM的镜像包,借此用源码编译进行测试。

坚持就是胜利的博客 1010

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

【用unity实现100个游戏之15】开发一个类保卫萝卜的Unity2D塔防游戏5(附项目源码,完结)

本期是本项目的最后一篇,主要内容是配置环境、生成不同敌人、结束重开。

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 2575

[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

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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、付费专栏及课程。

余额充值