当前位置:网站首页>做题笔记2(两数相加)
做题笔记2(两数相加)
2022-07-28 16:02:00 【竹林观月】
一、题目
二、思路
1
引入carry,默认为0。
2
当两个链表处于同一位的值都不为零时,两数相加再加上carry的值,若为个位数将该数赋予该节点;若为十位数,则还需将该数的十位赋予carry。
3
若两个链表中的一个在该节点处为空,则用0代替。
4
两链表都为空时,若carry的值不为零,需在尾部再插入carry的值。
三、用c++实现迭代法
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
ListNode *head = nullptr, *tail = nullptr;
int carry = 0;
while (l1!=0 || l2!=0)
{
int n1 =0;
if(l1!=0)
{
n1=l1->val;
}
int n2=0;
if(l2!=0)
{
n2=l2->val;
}
int sum = n1 + n2 + carry;
if (head==0)
{
head =tail = new ListNode(sum % 10);
}
else
{
tail->next = new ListNode(sum % 10);
tail = tail->next;
}
carry = sum / 10;
if (l1!=0)
{
l1 = l1->next;
}
if (l2!=0)
{
l2 = l2->next;
}
}
if (carry > 0)
{
tail->next = new ListNode(carry);
}
return head;
}
};
边栏推荐
- How should I understand craft
- 局域网无法访问apache服务器
- Some suggestions on Oracle SQL tuning
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- The local area network cannot access the Apache server
- Simple addition, deletion, modification and query of commodity information
- 【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)
- [learn slam from scratch] publish the coordinate system transformation relationship to topic TF
- Splash (rendering JS service) introduction installation
- 每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...
猜你喜欢

Sort 5-count sort

What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?

Ansa secondary development - build ansa secondary development environment on Visual Studio code

USB产品(FX3、CCG3PA)的调试方法
![[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)](/img/e8/2044cae63fe2145ce6294cb1fdfaa0.png)
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)

Several methods of HyperMesh running script files

快速掌握 Kotlin 集合函数

Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)

Sort 4-heap sort and massive TOPK problem

Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
随机推荐
Analysis of echo service model in the first six chapters of unp
ticdc同步数据怎么设置只同步指定的库?
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)
排序1-插入排序与希尔排序
重置grafana登录密码为默认密码
FX3开发板 及 原理图
Asp.net large file block upload breakpoint resume demo
Interesting kotlin 0x06:list minus list
配置web服务器步骤详细记录(多有借鉴)
【JS】1394- ES2022 的 8 个实用的新功能
How to set ticdc synchronization data to only synchronize the specified library?
What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
排序3-选择排序与归并排序(递归实现+非递归实现)
Mysql与Oracle的13点区别
Detailed record of steps to configure web server (many references)
NoSQL introduction practice notes I
LeetCode-学会复杂带随机指针链表的题(详解)
Interesting kotlin 0x08:what am I
parseJson
Applet: get element node information