当前位置:网站首页>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;
}
};
边栏推荐
- Original God 2.6 server download and installation tutorial
- Ranger (I) preliminary perception
- LeetCode 3. 无重复字符的最长子串
- [Xiaobai chat cloud] suggestions on container transformation of small and medium-sized enterprises
- 虚假的暑假
- mysql 计算经纬度范围内的数据
- Introduction to database system Chapter 1 short answer questions - how was the final exam?
- SQL solves the problem of continuous login deformation holiday filtering
- 历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
- PyC file decompile
猜你喜欢

Memory alignment of structure

Summary of multithreading and thread synchronization knowledge

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

数学分析_笔记_第6章:一元函数的Riemann积分

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

通过两级网关设计来路由服务网格流量

Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function

Recalling the college entrance examination and becoming a programmer, do you regret it?
![[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array](/img/51/f9c1eed37794db8c8d0eefd60b9e3d.jpg)
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array

自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
随机推荐
Data security industry series Salon (III) | data security industry standard system construction theme Salon
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
Add user-defined formula (time sharing t+0) to mobile app access as an example
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
JS learning notes - process control
sim2real环境配置教程
TypeScript数组乱序输出
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
Take you ten days to easily complete the go micro service series (I)
LeetCode 4. 寻找两个正序数组的中位数(hard)
[fluent] dart data type boolean type (boolean type definition | logical operation)
MySQL min() finds the minimum value under certain conditions, and there are multiple results
结构体的内存对齐
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
Ranger (I) preliminary perception
原神2.6服务端下载以及搭建安装教程
Figure database | Nepal graph v3.1.0 performance report
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
SSM整合-异常处理器及项目异常处理方案
AcWing 300. Task arrangement