当前位置:网站首页>[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
边栏推荐
- Do you choose pandas or SQL for the top 1 of data analysis in your mind?
- cuda编程
- On file uploading of network security
- golang 根据生日计算星座和属相
- 红米k40s root玩机笔记
- Gpt-3 is a peer review online when it has been submitted for its own research
- PHP lightweight Movie Video Search Player source code
- 自适应非欧表征广告检索系统AMCAD
- Index of MySQL
- 未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
猜你喜欢
运算放大器应用汇总1
Probability formula
【OA】Excel 文档生成器: Openpyxl 模块
Docker部署Mysql8的实现步骤
史上最全学习率调整策略lr_scheduler
史上最全MongoDB之Mongo Shell使用
Tencent cloud native database tdsql-c was selected into the cloud native product catalog of the Academy of communications and communications
AVL树插入操作与验证操作的简单实现
ABAP Dynamic Inner table Group cycle
On file uploading of network security
随机推荐
easyui出口excel无法下载框弹出的办法来解决
Antd comment recursive loop comment
Native MySQL
史上最全学习率调整策略lr_scheduler
Class constant pool and runtime constant pool
10 ways of interface data security assurance
【mysql】mysql中行排序
Kotlin Android environment construction
ABAP dynamic inner table grouping cycle
Probability formula
2022夏每日一题(一)
Food Chem|深度学习根据成分声明准确预测食品类别和营养成分
【安全攻防】序列化与反序列,你了解多少?
自适应非欧表征广告检索系统AMCAD
Calculation of time and space complexity (notes of runners)
cuda编程
一些常用软件相关
MySQL storage engine
使用 BR 备份 TiDB 集群到 GCS
idea gradle lombok 报错集锦