LeetCode2 Add Two Numbers

LeetCode --- 2. Add Two Numbers 解题报告 2. Add Two Numbers 解题报告 阅读详情
题目链接:

https://leetcode.com/problems/add-two-numbers/

题目描述:

给两个链表,代表两个非负数。逆序存储在链表节点中,例如(2 -> 4 -> 3)代表342。
将两个数相加,即两个链表相加。
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

分析:

水题,其实就是链表的大数加法。诶,但是犯了几个小错误。

ptr1->val = (ptr1->val + ptr2->val + num) % 10;
ptr2->val = (ptr1->val + ptr2->val + num) % 10;

这样写是不对的,第一行代码已经将ptr1->val的值改变了,第二行是用改变之后的ptr1->val在计算。

嗯~~~QAQ代码有点冗余,等我考完试再想想能不能改改。T-T

代码:
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* ptr1=l1;
        ListNode* ptr2=l2;
        int cnt=0;
        int num=0;
        ListNode* ptr1_pre=l1;
        ListNode* ptr2_pre=l2;
        while(ptr1 && ptr2){
           num=cnt;
           cnt=(ptr1->val+ptr2->val+cnt)/10;
           int val1=(ptr1->val+ptr2->val+num)%10;
           int val2=(ptr1->val+ptr2->val+num)%10;
           ptr1->val=val1;
           ptr2->val=val2;
           ptr1_pre=ptr1;
           ptr2_pre=ptr2;
           ptr1=ptr1->next;
           ptr2=ptr2->next;
        }
        if(ptr1){
           while(ptr1){
               if(cnt){
                  num=cnt;
                  cnt=(ptr1->val+cnt)/10;
                  ptr1->val=(ptr1->val+num)%10;
               }
               ptr1_pre=ptr1;
               ptr1=ptr1->next;
          }
          if(cnt){
               ListNode* pNew=(ListNode*)malloc(sizeof(ListNode));
               pNew->val=cnt;
               pNew->next=NULL;
               ptr1_pre->next=pNew;
           }
           return l1;
        }
        else if(ptr2){
           while(ptr2){
               if(cnt){
                  num=cnt;
                  cnt=(ptr2->val+cnt)/10;
                  ptr2->val=(ptr2->val+num)%10;
               }
               ptr2_pre=ptr2;
               ptr2=ptr2->next;
          }
          if(cnt){
               ListNode* pNew=(ListNode*)malloc(sizeof(ListNode));
               pNew->val=cnt;
               pNew->next=NULL;
               ptr2_pre->next=pNew;
           }
           return l2;
        }
        else{
            if(cnt){
               ListNode* pNew=(ListNode*)malloc(sizeof(ListNode));
               pNew->val=cnt;
               pNew->next=NULL;
               ptr1_pre->next=pNew;
           }
            return l1;
        }

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

相关推荐

Eureka常见面试题总结

点击关注我的博客原文 Eureka是Netflix组件的一个子模块,也是核心模块之一。云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移(来源springcloud中文网的介绍:https://www.springcloud.cc/)。下图总结了Eureka服务端(以下简称服务端)与Eureka客户端(以下简称客户端)之间协同工作的流程: 流程说明:...

u012105931的博客 2万+

LeetCode 2. Add Two Numbers(C++)

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

赞美月亮的专栏 1435

从零开始:小鼠Bulk RNA-seq全流程实战指南

本文提供了一份详尽的小鼠Bulk RNA-seq全流程实战指南,涵盖从软件环境搭建、原始数据质控与清洗、序列比对、基因定量到下游差异表达分析与功能富集。内容面向生物信息学新手,通过清晰的代码示例和步骤解析,帮助读者系统掌握Bulk RNA-seq数据分析的核心技能,并产出可靠的生物学洞见。

lake5的博客 715

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

RK3588 EVB开发板原理图讲解【六】

该原理图展示了eMMC Flash与主控芯片之间的连接关系,以及相关的电源、信号线路和去耦电容等。

weixin_48721556的博客 1165

K-GAS解读(08)

大众kgas标准,大众及其供应商kgas

m0_51113317的博客 598

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

通俗易懂的图解二叉搜索树、AVL树和红黑树

搜索是一种很常见的需求,当数据较多时,如何找到目标对象时,即需要搜索查找。 那么如何快速找到目标呢,最朴素的思想就是暴力查找,一个一个对比。 显然,当数据量增大时,我们需要优化查找方法获取更快的查找速度,我们可以通过维护一个有序数组,从而实现二分查找,查找速度从O(n)降为O(longn)。 但是数组并不便于插入和删除,链表便成为了最好的选择。 既然是采用二分查找,那么不如直接按照查找的步骤维护一种数据结构,于是二叉搜索树出现了。 二叉搜索树的性质为: 若左子树不为空,则左子树上所有节点值都小于根结点值;

暗色光的博客 758

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

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

weixin_41554427的博客 442

如何加载交易策略EA到MT4/MT5

1、打开MT4/MT5软件 2、首先,必须有一个EA,而且必须是mq4/ex4为扩展名。点击左上角“文件”—“打开数据文件夹”,找到安装路径 3、将EA的ex4或mq4文件放到MT4目录\MQL4\Experts里面(本文以名为Demo的EA为例) 4、如果有DLL库文件,复制到experts目录下的libraries子目录中 5、如果有tpl模板文件,复制到templates目录中 6、添加完成后重启MT4,点击允许EA运行图标 7、在导航中找到刚才安装的EA,拖拽到...

weixin_41364817的博客 7359

Android-音频配置文件详解

前言:   Android P 引入了一种新的音频政策配置文件格式 (XML),用于描述音频拓扑。新的 XML 文件支持定义输出输入流配置文件、可用于播放和捕获的设备以及音频属性的数量和类型。但是每个项目的代码里面都有很多音频配置文件,具体使用的是哪个文件呢?本文将详细讲述。 1. 查看运行时具体使用的xml adb shell dumpsys media.audio_policy | grep -iE "Config source:" 结果如下: Config source: /vendor

cheriyou_的博客 3944

LeetCode-2. Add Two Numbers

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

public Blog myBlog(){ 1125

vue3 - 高德地图定位出错Get ipLocation failed.Geolocation permission denied.(info: “FAILED“ | status: 0)报错解决!

vue3,nuxt3,高德地图,定位,位置,报错,AMap.Geolocation,解决报当前定位失败,高德地图H5 定位失败报错,web网页网站,报错详细解决教程,Get ipLocation failed.Geolocation permission denied.,高德地图定位报错info: "FAILED",高德地图status: 0,已经是https了还是报错,确保了浏览器位置已经开了,手机定位也开了,没解决,H5页面在ios的浏览器上使用 高德地图 报当前定位失败Geolocation,移动端设

✅ 精选推荐文章 6771

[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

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

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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

[LeetCode] 2. Add Two Numbers

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

ZY的博客 301

NTscan扫描工具

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

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

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

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

力扣Leetcode 2 两数相加 Add Two Numbers

暴力法 递归法

qq_39674417的博客 177

【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

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

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

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

hgq522的博客 380

[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

Leetcode 2. Add Two Numbers

题目链接:2. Add Two Numbers 题目大意:给你由两个链表组成得数,链表上得每个位置得值代表一个位得值,例如3->8->4就代表483这个值,然后两个链表相加,返回结果。 题目思路:简单题,每次相加得时候记录一次进位就可以了,只不过处理链表得时候比较麻烦,具体看代码 时间复杂度&&空间复杂度:O(max(n,m))&&O(n+m)(n,m分...

Nemaleswang的博客 306

leetcode记录2.Add Two Numbers

leetcode记录2.Add Two Numbers # # @lc app=leetcode id=2 lang=python3 # # [2] Add Two Numbers # # https://leetcode.com/problems/add-two-numbers/description/ # # algorithms # Medium (30.58%) # Total Accep...

我的博客 212
上一篇: LeetCode142 Linked List Cycle II
下一篇: LeetCode113 Path Sum II
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值