当前位置:网站首页>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;
}
};
边栏推荐
- Route service grid traffic through two-level gateway design
- Which software is good for machine vision?
- dried food! Understand the structural vulnerability of graph convolution networks
- JS learning notes - data types
- Yyds dry inventory KVM new inventory to expand space for home
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- JS learning notes - variables
- LeetCode 1. 两数之和
- Yyds dry inventory method of deleting expired documents in batch
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
猜你喜欢

⌈ 2022 ⌋ how to use webp gracefully in projects

忆当年高考|成为程序员的你,后悔了吗?
![[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

The light of ideal never dies

What if the win11 app store cannot load the page? Win11 store cannot load page

Vscade set multi line display of tab

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

LeetCode 1. 两数之和

What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?

Ranger (I) preliminary perception
随机推荐
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
Summary of monthly report | list of major events of moonbeam in June
Mobile web development learning notes - Layout
绝对真理和相对真理思考
JS learning notes - data types
Yyds dry inventory KVM new inventory to expand space for home
数学分析_笔记_第6章:一元函数的Riemann积分
What is normal distribution? What is the 28 law?
忆当年高考|成为程序员的你,后悔了吗?
Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
Maui learning road (III) -- in depth discussion of winui3
结构体的内存对齐
电脑管理员权限在哪里可以打开
月报总结|Moonbeam6月份大事一览
Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
LeetCode 1. 两数之和
Does bone conduction earphone have external sound? Advantages of bone conduction earphones
mysql min() 求某条件下最小的值出现多个结果
The difference and usage of calloc, malloc and realloc functions