当前位置:网站首页>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;
}
};
边栏推荐
- Aike AI frontier promotion (2.15)
- Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
- Source code look me
- Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- LeetCode 3. 无重复字符的最长子串
- Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
猜你喜欢

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

触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加

Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction

结构体的内存对齐

流批一体在京东的探索与实践

TypeScript数组乱序输出

Memory alignment of structure

Kubernetes family container housekeeper pod online Q & A?

Which software is good for machine vision?

Take you ten days to easily complete the go micro service series (I)
随机推荐
Summary of monthly report | list of major events of moonbeam in June
day4
OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
SSM integration exception handler and project exception handling scheme
Where can I open computer administrator permissions
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
Vscode设置标签页多行显示
学生选课系统(山东农业大学课程设计)
LeetCode 6. Z 字形变换 (N字形变换)
Written by unity Jason
请问怎么在oracle视图中使用stustr函数
IDEA中设置背景图片(超详细)
Sim2real environment configuration tutorial
通过两级网关设计来路由服务网格流量
Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
Some problems about MySQL installation
Practice of traffic recording and playback in vivo
MySQL min() finds the minimum value under certain conditions, and there are multiple results
The login box of unity hub becomes too narrow to log in