当前位置:网站首页>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 <( ̄︶ ̄)>
边栏推荐
- Database exception resolution caused by large table delete data deletion
- The rebound problem of using Scrollview in cocos Creator
- Starting from 1.5, build a microservice framework link tracking traceid
- 【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
- Introduction of mongod management database method
- Basic knowledge sorting of mongodb database
- 【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
- Pit avoidance: description of null values in in and not in SQL
- HW primary flow monitoring, what should we do
- 【OBS】RTMPSockBuf_ Fill, remote host closed connection.
猜你喜欢

Zhongang Mining: Fluorite continues to lead the growth of new energy market
![[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)](/img/7e/188e57ee026200478a6f61eb507c92.png)
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)

Steps to create P8 certificate and warehousing account

【数字IC验证快速入门】25、SystemVerilog项目实践之AHB-SRAMC(5)(AHB 重点回顾,要点提炼)
![[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)](/img/a3/7f08f189c608d6086b368dfa3831f7.png)
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)

【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)

Ida Pro reverse tool finds the IP and port of the socket server

2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码

Vertex shader to slice shader procedure, varying variable
![[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)](/img/d3/cab8a1cba3c8d8107ce4a95f328d36.png)
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
随机推荐
Nacos一致性协议 CP/AP/JRaft/Distro协议
unnamed prototyped parameters not allowed when body is present
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
Matlab experience summary
Cocos uses custom material to display problems
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
OpenGL's distinction and understanding of VAO, VBO and EBO
Annexb and avcc are two methods of data segmentation in decoding
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
[deep learning] image hyperspectral experiment: srcnn/fsrcnn
What is data leakage
Mathematical modeling -- what is mathematical modeling
webgl_ Enter the three-dimensional world (2)
Monthly observation of internet medical field in May 2022
Guangzhou Development Zone enables geographical indication products to help rural revitalization
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
TypeScript 发布 4.8 beta 版本