当前位置:网站首页>LeetCode2_ Add two numbers
LeetCode2_ Add two numbers
2022-07-07 15:42:00 【WhiteTian】
Original article , Reprint please indicate the source .
Topic type : secondary
C++ Explain Addition of two numbers
The title is as follows
Here are two for you Non empty The linked list of , Represents two nonnegative integers . Each of them is based on The reverse Stored in , And each node can only store a Numbers .
Please add up the two numbers , And returns a linked list representing sum in the same form .
You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
Example 1:
Input :l1 = [2,4,3], l2 = [5,6,4]
Output :[7,0,8]
explain :342 + 465 = 807.
Example 2:
Input :l1 = [0], l2 = [0]
Output :[0]
Example 3:
Input :l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output :[8,9,9,9,0,0,0,1]
Tips :
The number of nodes in each list is in the range [1, 100] Inside
0 <= Node.val <= 9
The title data guarantees that the number indicated in the list does not contain leading zeros
solution

Simple and easy to understand , Non recursive version
Complexity analysis
Time complexity :O(max(m,n)) , among m and n They are the length of two linked lists . We need to traverse all the positions of the two linked lists , And processing each location only requires O(1) Time for .
Spatial complexity :O(1). Note that the return value is not included in the spatial complexity .
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* pRoot = new ListNode(0);
ListNode* pCursor = pRoot;
int nCarry = 0;
while(l1 || l2 || nCarry > 0)
{
int l1Value = l1?l1->val:0;
int l2Value = l2?l2->val:0;
int nSum = l1Value+l2Value+nCarry;
nCarry = nSum/10;
ListNode* pNext = new ListNode(nSum%10);
pCursor->next = pNext;
pCursor = pNext;
if(l1)
l1 = l1->next;
if(l2)
l2 = l2->next;
}
return pRoot->next;
}
};
leetcode score 
thank you , It's not easy to create , Great Xia, please stay … Move your lovely hands , Give me a compliment before you go <( ̄︶ ̄)>
边栏推荐
- 居然从408改考自命题!211华北电力大学(北京)
- 2. Heap sort "hard to understand sort"
- 连接ftp服务器教程
- Zhongang Mining: Fluorite continues to lead the growth of new energy market
- 最安全的证券交易app都有哪些
- webgl_ Graphic transformation (rotation, translation, zoom)
- Unity's ASE achieves full screen sand blowing effect
- Steps to create P8 certificate and warehousing account
- Runnable是否可以中断
- 摘抄的只言片语
猜你喜欢
使用Scrapy框架爬取网页并保存到Mysql的实现

Getting started with webgl (4)

2. 堆排序『较难理解的排序』

【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)

The difference between full-time graduate students and part-time graduate students!

The rebound problem of using Scrollview in cocos Creator

【深度学习】图像超分实验:SRCNN/FSRCNN

如何在opensea批量发布NFT(Rinkeby测试网)

Summer safety is very important! Emergency safety education enters kindergarten

2022年5月互联网医疗领域月度观察
随机推荐
【深度学习】语义分割实验:Unet网络/MSRC2数据集
Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
Yunxiaoduo software internal test distribution test platform description document
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
连接ftp服务器教程
MySQL bit type resolution
Pat grade a 1103 integer factorizatio
【搞船日记】【Shapr3D的STL格式转Gcode】
Bye, Dachang! I'm going to the factory today
Cocos makes Scrollview to realize the effect of zooming in the middle and zooming out on both sides
【数字IC验证快速入门】26、SystemVerilog项目实践之AHB-SRAMC(6)(APB协议基本要点)
jacoco代码覆盖率
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
Spin animation of Cocos performance optimization
有钱人买房就是不一样
从 1.5 开始搭建一个微服务框架链路追踪 traceId
【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
Unity之ASE实现卡通火焰
Oracle control file loss recovery archive mode method
Annexb and avcc are two methods of data segmentation in decoding