当前位置:网站首页>LeetCode 2. 两数相加
LeetCode 2. 两数相加
2022-07-02 13:15:00 【_刘小雨】
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.
示例 2:
输入:l1 = [0], l2 = [0]
输出:[0]
示例 3:
输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:[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;
}
};
边栏推荐
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
- 分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
- Sim2real environment configuration tutorial
- Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
- False summer vacation
- 理想之光不灭
- Yyds dry inventory method of deleting expired documents in batch
- How to use stustr function in Oracle view
- 做机器视觉哪个软件好?
- Yyds dry inventory company stipulates that all interfaces use post requests. Why?
猜你喜欢

2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time

July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance

总结|机器视觉中三大坐标系及其相互关系

Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse

Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau

Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification

Understand the key technology of AGV -- the difference between laser slam and visual slam

Figure database | Nepal graph v3.1.0 performance report

Bib | graph representation based on heterogeneous information network learning to predict drug disease association

请问怎么在oracle视图中使用stustr函数
随机推荐
Take you ten days to easily complete the go micro service series (I)
Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
总结|机器视觉中三大坐标系及其相互关系
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
day4
Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau
LeetCode 1. 两数之和
月报总结|Moonbeam6月份大事一览
False summer vacation
关于mysql安装的一些问题
Mysql database mysqldump why there is no statement to create a database
Practice of constructing ten billion relationship knowledge map based on Nebula graph
Ranger (I) preliminary perception
Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
结构体的内存对齐
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
做机器视觉哪个软件好?
Introduction to database system Chapter 1 short answer questions - how was the final exam?
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation