当前位置:网站首页>[record of question brushing] 2 Add two numbers
[record of question brushing] 2 Add two numbers
2022-07-07 04:04:00 【InfoQ】
One 、 Title Description
- The number of nodes in each list is in the range [1, 100] Inside
0 <= Node.val <= 9
- The title data guarantees that the number indicated in the list does not contain leading zeros
Two 、 Thought analysis :
- l1 Namely [3,4] l2 Namely [7,2] The addition of corresponding positions in the linked list ,In the process of numerical operation , A process of adding from low to high .
- If there is a carry digit in addition , We need to record the number of digits , And add this carry number in one run It's the one in the picture above1(tens)
- Then save the results of each time into a new linked list For example, the result in the above figure is 70 Namely [0,7]
3、 ... and 、 Code implementation
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Linked list of output results
ListNode resultNode = new ListNode(0);
ListNode head = resultNode;
// Record carry digit
int carryNum = 0;
while (l1 != null || l2 != null){
// Judge whether there is a value in the corresponding position in the two linked lists ( Maybe even the digits of the linked list are inconsistent )
int num1 = l1 != null ? l1.val : 0;
int num2 = l2 != null ? l2.val : 0;
// Add the numbers in the corresponding position And add the carry digit
int tmpnum = num1 + num2 + carryNum;
// Record the corresponding number to the linked list
head.next = new ListNode(tmpnum % 10 );
// Get carry
carryNum = tmpnum / 10;
head = head.next;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
}
// If there is a carry at the top Fill a .
if (carryNum > 0) head.next = new ListNode(carryNum);
return resultNode.next;
}
}
summary
边栏推荐
- Antd Comment 递归循环评论
- 【安全攻防】序列化與反序列,你了解多少?
- How to detect whether the MySQL code runs deadlock +binlog view
- Redis源码学习(31),字典学习,dict.c(一)
- Machine learning notes - bird species classification using machine learning
- Calculation of time and space complexity (notes of runners)
- One of oscp tools: dirsearch usage Encyclopedia
- UltraEdit-32 温馨提示:右协会,取消 bak文件[通俗易懂]
- [hcie TAC] question 3
- 学习使用js把两个对象合并成一个对象的方法Object.assign()
猜你喜欢
数据的存储
Redis configuration and optimization of NoSQL
未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
Simple implementation of AVL tree insertion and verification operations
Mobile measurement and depth link platform - Branch
Kotlin Android 环境搭建
ABAP Dynamic Inner table Group cycle
Arduino droplet detection
[security attack and Defense] how much do you know about serialization and deserialization?
NoSQL之Redis配置与优化
随机推荐
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
vim —- 自己主动的按钮indent该命令「建议收藏」
Redis源码学习(30),字典学习,dict.h
1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
Operational amplifier application summary 1
使用Thread类和Runnable接口实现多线程的区别
史上最全MongoDB之Mongo Shell使用
The most complete learning rate adjustment strategy in history LR_ scheduler
Introduction to opensea platform developed by NFT trading platform (I)
红米k40s root玩机笔记
PHP implements lottery according to probability
It's too convenient. You can complete the code release and approval by nailing it!
codeforces每日5题(均1700)-第七天
【OA】Excel 文档生成器: Openpyxl 模块
Learn how to use js to merge two objects into one object assign()
10 ways of interface data security assurance
Triple half circle progress bar, you can use it directly
MySQL data loss, analyze binlog log file
AVL树插入操作与验证操作的简单实现
使用 Dumpling 备份 TiDB 集群数据到 GCS