当前位置:网站首页>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 <( ̄︶ ̄)>
边栏推荐
- The rebound problem of using Scrollview in cocos Creator
- OpenGL's distinction and understanding of VAO, VBO and EBO
- [deep learning] image hyperspectral experiment: srcnn/fsrcnn
- 众昂矿业:萤石继续引领新能源市场增长
- [markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
- Wechat applet 01
- Database exception resolution caused by large table delete data deletion
- 【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
- Jacobo code coverage
- How to understand that binary complement represents negative numbers
猜你喜欢
有钱人买房就是不一样
Bye, Dachang! I'm going to the factory today
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
Steps to create P8 certificate and warehousing account
The difference between full-time graduate students and part-time graduate students!
Wechat applet 01
What is Base64?
Zhongang Mining: Fluorite continues to lead the growth of new energy market
The rebound problem of using Scrollview in cocos Creator
Briefly describe the working principle of kept
随机推荐
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
【数字IC验证快速入门】26、SystemVerilog项目实践之AHB-SRAMC(6)(APB协议基本要点)
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
What is data leakage
STM32F103C8T6 PWM驱动舵机(SG90)
Steps to create P8 certificate and warehousing account
Share the technical details of super signature system construction
Wechat applet 01
Use cpolar to build a business website (2)
The download button and debug button in keil are grayed out
How to deploy the super signature distribution platform system?
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
Starting from 1.5, build a microservice framework link tracking traceid
PAT 甲级 1103 Integer Factorizatio
Nacos conformance protocol cp/ap/jraft/distro protocol
Runnable是否可以中断
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation