当前位置:网站首页>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 <( ̄︶ ̄)>
边栏推荐
- 【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
- PAT 甲级 1103 Integer Factorizatio
- [quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
- Use cpolar to build a business website (2)
- Do not use memset to clear floating-point numbers
- The significance of XOR in embedded C language
- 从 1.5 开始搭建一个微服务框架链路追踪 traceId
- MySQL bit类型解析
- 【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
- 简述keepalived工作原理
猜你喜欢

TypeScript 发布 4.8 beta 版本
![[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)](/img/91/16a370ac41adc8fe31507765a82b0a.png)
[quick start of Digital IC Verification] 18. Basic grammar of SystemVerilog learning 5 (concurrent threads... Including practical exercises)

Cocos uses custom material to display problems

2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code

MySQL bit类型解析

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

webgl_ Enter the three-dimensional world (1)

Mathematical modeling -- what is mathematical modeling

全日制研究生和非全日制研究生的区别!
Implementation of crawling web pages and saving them to MySQL using the scrapy framework
随机推荐
Unity's ASE achieves full screen sand blowing effect
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
【搞船日记】【Shapr3D的STL格式转Gcode】
2. Heap sort "hard to understand sort"
How to create Apple Developer personal account P8 certificate
简述keepalived工作原理
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
Streaming end, server end, player end
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
Super signature principle (fully automated super signature) [Yun Xiaoduo]
The significance of XOR in embedded C language
Getting started with webgl (3)
Unity's ASE realizes cartoon flame
Bye, Dachang! I'm going to the factory today
Configure mongodb database in window environment
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
Share the technical details of super signature system construction
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
jacoco代码覆盖率
Android -- jetpack: the difference between livedata setValue and postvalue