当前位置:网站首页>[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
边栏推荐
猜你喜欢

Kotlin Android environment construction

Some common software related

【编码字体系列】OpenDyslexic字体

【开发软件】 tilipa开发者软件

Summer 2022 daily question 1 (1)

Clock in during winter vacation

1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数

Antd Comment 递归循环评论

【刷题记录】2. 两数相加

如何检测mysql代码运行是否出现死锁+binlog查看
随机推荐
ggplot 分面的细节调整汇总
PHP 实现根据概率抽奖
【刷题记录】2. 两数相加
使用切面实现记录操作日志
web服务性能监控方案
Native MySQL
Force buckle ----- path sum III
Redis configuration and optimization of NoSQL
Redis源码学习(31),字典学习,dict.c(一)
Preprocessing - interpolation
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
PHP implements lottery according to probability
Hongmi K40S root gameplay notes
学习使用js把两个对象合并成一个对象的方法Object.assign()
Implementation steps of docker deploying mysql8
Codeworks 5 questions per day (1700 average) - day 7
Create commonly used shortcut icons at the top of the ad interface (menu bar)
codeforces每日5题(均1700)-第七天
Construction of Hisilicon universal platform: color space conversion YUV2RGB
Enter the rough outline of the URL question (continuously updated)