当前位置:网站首页>[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
边栏推荐
- termux设置电脑连接手机。(敲打命令贼快),手机termux端口8022
- Triple half circle progress bar, you can use it directly
- The JSON format of the international area code of the mobile phone number is obtained with PHP
- ABAP 动态内表分组循环
- 使用切面实现记录操作日志
- Mobile measurement and depth link platform - Branch
- Index of MySQL
- Class constant pool and runtime constant pool
- golang 压缩和解压zip文件
- Food Chem|深度学习根据成分声明准确预测食品类别和营养成分
猜你喜欢
随机推荐
First understand the principle of network
Preprocessing - interpolation
AVL树插入操作与验证操作的简单实现
ABAP 動態內錶分組循環
Quick completion guide of manipulator (10): accessible workspace
API data interface of A-share index component data
OSCP工具之一: dirsearch用法大全
【knife-4j 快速搭建swagger】
easyui出口excel无法下载框弹出的办法来解决
. Net interface can be implemented by default
手机号国际区号JSON格式另附PHP获取
Restore backup data on GCS with br
数据的存储
Native MySQL
Construction of Hisilicon universal platform: color space conversion YUV2RGB
Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
如何检测mysql代码运行是否出现死锁+binlog查看
QT 打开文件 使用 QFileDialog 获取文件名称、内容等
[leetcode] 700 and 701 (search and insert of binary search tree)
idea gradle lombok 报错集锦