当前位置:网站首页>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 <( ̄︶ ̄)>
边栏推荐
- Super signature principle (fully automated super signature) [Yun Xiaoduo]
- [quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
- Steps to create P8 certificate and warehousing account
- How to understand that binary complement represents negative numbers
- 【兰州大学】考研初试复试资料分享
- OpenGL's distinction and understanding of VAO, VBO and EBO
- Create lib Library in keil and use lib Library
- 15. Using the text editing tool VIM
- [server data recovery] a case of RAID data recovery of a brand StorageWorks server
- [quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
猜你喜欢

Wechat applet 01

2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
![[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset](/img/69/9dadeb92f8d6299250a894690c2845.png)
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset

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

银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
![[target detection] yolov5 Runtong voc2007 data set](/img/b3/b7f3d46075cb1782d772a24362003e.png)
[target detection] yolov5 Runtong voc2007 data set
![[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper](/img/38/cc5bb5eaa3dcee5ae2d51a904cf26a.png)
[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper

Qu'est - ce qu'une violation de données

【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
![[server data recovery] a case of RAID data recovery of a brand StorageWorks server](/img/aa/6d820d97e82df1d908dc7aa78fc8bf.png)
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
随机推荐
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
Guangzhou Development Zone enables geographical indication products to help rural revitalization
MongoDB数据库基础知识整理
简述keepalived工作原理
【深度学习】图像超分实验:SRCNN/FSRCNN
Implementation of crawling web pages and saving them to MySQL using the scrapy framework
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
Ida Pro reverse tool finds the IP and port of the socket server
What is data leakage
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Streaming end, server end, player end
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
使用cpolar建立一个商业网站(2)
Gd32 F3 pin mapping problem SW interface cannot be burned
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
The rebound problem of using Scrollview in cocos Creator
Nacos一致性协议 CP/AP/JRaft/Distro协议
What are PV and UV? pv、uv
Webgl texture
MySQL bit type resolution