LeetCode——Add Two Numbers

QGroundControl或者华科尔地面站安装完打不开问题解决 QGroundControl 华科尔 打不开 阅读详情

LeetCode——Add Two Numbers

leetcode上第二题:


题目内容:

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

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

题目翻译:

给定两个链表,它们代表两个非0的数,数字以逆序存在链表中,链表的每个结点包含一个单一的数。把这两个整数相加并以链表的形式返回。

解题思路:

主要考察链表的操作,两数的各位相加时注意进位。最后相加时注意最高位的值是否为1(即次高位想加时是否产生进位)感觉没有考察具体的算法。

<span style="font-size:14px;">/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode rootNode(0);
		ListNode *pCurNode = &rootNode;
		int index = 0;
		while(l1||l2)
		{
			int a = (l1?l1->val:0);
			int b = (l2?l2->val:0);
			int sum = a+b+index;
			index = sum/10;
			sum %= 10;
			ListNode *newNode = new ListNode(sum);
			pCurNode->next = newNode;
			pCurNode = newNode;
			if(l1)
				l1 = l1->next;
			if(l2)
				l2 = l2->next;
		}
		if(index>0)
		{
			ListNode *newNode = new ListNode(index);
			pCurNode->next = newNode;
		}
		return rootNode.next;
    }
};</span>







uniapp:APP报错Error: Not Found:Page[2][-1,1;-1,1,0,0,2-0] at view.umd.min.js:1(uniApp安卓苹果ios出现一大堆数字错误) Error: Not Found:Page[2][一串数字] at view.umd.min.js:1,uniapp在APP端运行报错Error: Not Found:Page[2][-1,1;-1,1,0,0,2-0] at view.umd.min.js:1,uniapp真机运行偶尔出现Not Found:Page[2][-1,28] at view.umd.min.js:1,如何解决uniApp app报错:Not found -1,71 at view.umd.min.js:1,uniapp项目在H 阅读详情

相关推荐

【OpenAirInterface知识-3】OAI端到端部署之5G基站部署

在上一篇文章中已经完成了5G核心网的部署,本篇文章将介绍OAI 5G基站的部署过程。

起名字难上难的博客 3421

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

简话 RAG 本地部署(DeepSeek + RAGFlow)

RAG = DeepSeek + OLlama + Docker + RAGFlow + Windows 11 简话 RAG 本地部署

捉虫客 de 博客 1万+

leetcode Add Two Numbers

最近一直在忙实验室项目的事情,好久没写代码了,手太生了,所以去leetcode官网随便找了两个简单题刷了一下,第一个一遍就通过了,第二个还是有点问题。感觉这种东西就得多练。不然会一直退步退步,下面是我写的两个题的解法,仅供参考:(先写一题吧下一题之后补上) Add Two  Numbers //C/C++解法 /**  * Definition for singly-linked

yaojianpengshr的博客 658

QQ技术大总结

此贴只是一些对q友有用的东西,也许有人知道,高手勿喷。进如正题~ 1. 进入访问受限的qq空间 在地址栏里输入 http://qqxoo.com/main.html?qqid=qq号这样就可能看对方的空间 http://qqshow-user.tencent.com/qq号/11/00 看mm的qq绣 http://shuqian.qq.com/qq号/ qq书签 ...

weixin_33845881的博客 1270

数学建模小白必看:层次分析法(AHP)从入门到实战(附Python代码)

本文详细介绍了层次分析法(AHP)在数学建模竞赛中的应用,从构建判断矩阵到一致性检验,再到权重计算和实战案例。通过Python代码实现,帮助小白快速掌握AHP的核心技巧,解决评价类难题,提升建模效率。

web39explorer的博客 918

ACM之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 two numbers and re

Microstrong 657

无网络环境部署:手动获取并安装OneNote for Windows 10完整指南

本文提供了一份详尽的OneNote for Windows 10离线安装指南。针对无网络或内网隔离环境,详细介绍了如何从官方渠道获取正确的产品ID,利用第三方工具下载纯净的.appxbundle安装包,并解决系统兼容性与依赖框架问题。最后,通过PowerShell管理员命令完成安全部署,是IT管理员和高级用户在离线环境下部署UWP应用的实用参考。

loss4bartender的博客 495

LeetCode 2. Add Two Numbers(C++)

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

赞美月亮的专栏 1435

C++ (week6、7):Linux系统编程4:网络

Linux网络:计算机网络概述、TCP协议、Socket编程、IO多路复用机制(select、epoll)、HTTP协议、进程池、线程池、大文件网络传输问题

Edward2022的博客 1430

Oracle EBS的帐套由“4C”构成,而华为MetaERP将其发展为“6C”

华为MetaERP从OracleEBS的"4C"架构升级为"6C",实现核算理念革新。核心突破在于将会计科目、核算维度和核算单元解耦,形成"科目表+维度库"的灵活架构。新架构优势显著:1)科目回归会计本质,管理维度独立扩展;2)支持无限维度的精细化核算;3)实现数据权限隔离和内部交易自动化;4)采用元数据驱动设计,为AI应用奠定基础。这一平台化架构解决了大型企业在全球化、多元化发展中的核算难题,相比传统架构具有更强的灵活性和扩展性。

金牌架构师 204

LeetCode //C - 445. Add Two Numbers II

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

Made in Code 1897

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LeetCode-Add Two Numbers(JS题解)

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

爪蛙没有爪的博客 322

IDEA的使用指南

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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【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力扣之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 i...

LeeWei 268

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

LeetCode 445 Add Two Numbers II

LeetCode 445 Add Two Numbers II Problem Description: 对给出的两条非空链表结点值按照从尾到头的顺序进行相加 具体的题目信息: https://leetcode.com/problems/add-two-numbers-ii/description/ Solution: 解题思路:从头开始遍历链表,分别用两个栈存储两条链表各个节点的...

萤火虫啊飞呀飞 329

LeetCode Add Two Numbers

题目链接:http://oj.leetcode.com/problems/add-two-numbers/ 描述: Add Two Numbers   You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order a

wyn_HELLOWORLD的专栏 775

Leetcode02. Add Two Numbers Medium

求两个链表表示的数相加

magic_jiayu的博客 278

LeetCode:Add Two Numbers

Add Two Numbers - LeetCode

杂的文 639

[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

Add Two Numbers II || LeetCode-445

Add Two Numbers II || LeetCode-445 Problem link:https://leetcode.com/problems/add-two-numbers-ii/#/description You are given two non-empty linked lists representing two non-negative integers

Be a learner Not a knower. 231

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

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

weixin_41554427的博客 442

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
上一篇: LeetCode——Two Sum
下一篇: LeetCode——Longest Substring Without Repeating Characters
cassiePython
博客等级 码龄12年 303粉丝 135原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值