当前位置:网站首页>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;
}
};
边栏推荐
- TCP拥塞控制详解 | 2. 背景
- TCP server communication process (important)
- Take you ten days to easily complete the go micro service series (I)
- Written by unity Jason
- Yyds dry inventory method of deleting expired documents in batch
- Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
- 流批一体在京东的探索与实践
- OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
- 七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
- Maui学习之路(三)--Winui3深入探讨
猜你喜欢

SSM integration exception handler and project exception handling scheme

大厂面试总结大全

The light of ideal never dies

mysql min() 求某条件下最小的值出现多个结果

结构体的内存对齐

JS learning notes - process control

unity Hub 登錄框變得很窄 無法登錄

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

LeetCode 1. 两数之和

Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
随机推荐
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
AWS virtual machine expansion
Some problems about MySQL installation
Set the background picture in the idea (ultra detailed)
Download blender on Alibaba cloud image station
Source code look me
sql解决连续登录问题变形-节假日过滤
Vscode设置标签页多行显示
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
数学分析_笔记_第6章:一元函数的Riemann积分
Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
Maui learning road (III) -- in depth discussion of winui3
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
大厂面试总结大全
[fluent] dart data type string type (string definition | string splicing | string API call)
Student course selection system (curriculum design of Shandong Agricultural University)
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
Mobile web development learning notes - Layout
Yyds dry inventory executor package (parameter processing function)
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~