当前位置:网站首页>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;
}
};
边栏推荐
- The light of ideal never dies
- Unity Json 编写
- 总结|机器视觉中三大坐标系及其相互关系
- (practice C language every day) the sum of the nearest three numbers
- Vscade set multi line display of tab
- Classic quotations
- Sqlserver queries which indexes are underutilized
- 机器学习-感知机模型
- 忆当年高考|成为程序员的你,后悔了吗?
- Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
猜你喜欢

Where can I open computer administrator permissions

请问怎么在oracle视图中使用stustr函数

历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...

做机器视觉哪个软件好?

七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板

Yyds dry goods inventory student attendance system based on QT design

How to choose the right kubernetes storage plug-in? (09)

unity Hub 登录框变得很窄 无法登录

TypeScript数组乱序输出

SSM整合-异常处理器及项目异常处理方案
随机推荐
Route service grid traffic through two-level gateway design
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
请问怎么在oracle视图中使用stustr函数
Data security industry series Salon (III) | data security industry standard system construction theme Salon
Maui learning road (III) -- in depth discussion of winui3
Summary | three coordinate systems in machine vision and their relationships
dried food! Understand the structural vulnerability of graph convolution networks
2022最新最详细必成功的在Vscode中设置背景图、同时解决不受支持的问题
Sqlserver queries which indexes are underutilized
Aike AI frontier promotion (2.15)
Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
Effectively use keywords to increase Amazon sales
[fluent] dart data type string type (string definition | string splicing | string API call)
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
[fluent] dart data type boolean type (boolean type definition | logical operation)
去除router-link中的下划线
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
绝对真理和相对真理思考
虚假的暑假
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share