当前位置:网站首页>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;
}
};
边栏推荐
- PCL point cloud image transformation
- What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
- Typescript array out of order output
- JS learning notes - process control
- Take you ten days to easily complete the go micro service series (I)
- dried food! Understand the structural vulnerability of graph convolution networks
- Cloud native cicd framework: Tekton
- TypeScript数组乱序输出
- Seal Library - installation and introduction
- Remove the underline in router link
猜你喜欢
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
PCL point cloud image transformation
Kubernetes family container housekeeper pod online Q & A?
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
Maui learning road (III) -- in depth discussion of winui3
TCP server communication process (important)
Route service grid traffic through two-level gateway design
Download blender on Alibaba cloud image station
TCP拥塞控制详解 | 2. 背景
Memory alignment of structure
随机推荐
Typescript array out of order output
学生选课系统(山东农业大学课程设计)
Yyds dry inventory KVM new inventory to expand space for home
电脑管理员权限在哪里可以打开
Summary | three coordinate systems in machine vision and their relationships
OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
SQL solves the problem of continuous login deformation holiday filtering
Unity Json 编写
sql解决连续登录问题变形-节假日过滤
Everyone Xinfu builds: a one-stop intelligent business credit service platform
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
数学分析_笔记_第5章:一元微分学
Memory alignment of structure
Thinking about absolute truth and relative truth
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
结构体的内存对齐
Where can I open computer administrator permissions
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
Summary of multithreading and thread synchronization knowledge