当前位置:网站首页>LeetCode 2. Add two numbers
LeetCode 2. Add two numbers
2022-07-02 16:40:00 【_ Liu Xiaoyu】
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 .
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]
/** * 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* ret = new ListNode(0);
ListNode* cur = ret;
int sum = 0;
while(1)
{
if(l1 != nullptr)
{
sum += l1->val;
l1 = l1->next;
}
if(l2 != nullptr)
{
sum += l2->val;
l2 = l2->next;
}
cur->val = sum % 10;
sum /= 10;
if(l1 != nullptr || l2 != nullptr || sum)
cur = (cur->next = new ListNode(0));
else
break;
}
return ret;
}
};
边栏推荐
- 学生选课系统(山东农业大学课程设计)
- mysql min() 求某条件下最小的值出现多个结果
- How to choose the right kubernetes storage plug-in? (09)
- What is normal distribution? What is the 28 law?
- Set the background picture in the idea (ultra detailed)
- Take you ten days to easily complete the go micro service series (I)
- Aike AI frontier promotion (2.15)
- ⌈ 2022 ⌋ how to use webp gracefully in projects
- LeetCode 6. Z 字形变换 (N字形变换)
- Sim2real environment configuration tutorial
猜你喜欢
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
Routing mode: hash and history mode
Recalling the college entrance examination and becoming a programmer, do you regret it?
The login box of unity hub becomes too narrow to log in
How to use stustr function in Oracle view
忆当年高考|成为程序员的你,后悔了吗?
[fluent] dart data type string type (string definition | string splicing | string API call)
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
结构体的内存对齐
随机推荐
Vscode设置标签页多行显示
Sim2real environment configuration tutorial
Yyds dry inventory executor package (parameter processing function)
Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
Some problems about MySQL installation
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
PCL 点云镜像变换
Source code look me
历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
云原生的 CICD 框架:Tekton
AcWing 300. Task arrangement
Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus
大厂面试总结大全
JS learning notes - process control
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
图书管理系统(山东农业大学课程设计)
Maui learning road (III) -- in depth discussion of winui3
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share