当前位置:网站首页>[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
边栏推荐
- Redis源码学习(30),字典学习,dict.h
- 如何检测mysql代码运行是否出现死锁+binlog查看
- It's too convenient. You can complete the code release and approval by nailing it!
- Arduino droplet detection
- 浅谈网络安全之文件上传
- golang 压缩和解压zip文件
- QT item table new column name setting requirement exercise (find the number and maximum value of the array disappear)
- Redis source code learning (30), dictionary learning, dict.h
- 使用 BR 备份 TiDB 集群到 GCS
- 未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
猜你喜欢

链表面试常见题

Some thoughts on cross end development of kbone and applet

如何检测mysql代码运行是否出现死锁+binlog查看

ABAP Dynamic Inner table Group cycle

Construction of Hisilicon universal platform: color space conversion YUV2RGB

Simple implementation of AVL tree insertion and verification operations

opencv第三方库
![[development software] tilipa Developer Software](/img/b8/de2a1ea6474bb3f9b44e7ea01c441b.png)
[development software] tilipa Developer Software
接口数据安全保证的10种方式
![[MySQL] row sorting in MySQL](/img/97/8a451fa62796838e11242c86eecd8d.png)
[MySQL] row sorting in MySQL
随机推荐
【mysql】mysql中行排序
使用切面实现记录操作日志
Introduction to opensea platform developed by NFT trading platform (I)
Redis configuration and optimization of NoSQL
Web service performance monitoring scheme
QT opens a file and uses QFileDialog to obtain the file name, content, etc
What is Ba? How about Ba? What is the relationship between Ba and Bi?
自适应非欧表征广告检索系统AMCAD
太方便了,钉钉上就可完成代码发布审批啦!
Allow public connections to local Ruby on Rails Development Server
Use br to back up tidb cluster to GCS
【knife-4j 快速搭建swagger】
Class常量池与运行时常量池
Native MySQL
PHP 实现根据概率抽奖
Preprocessing - interpolation
Redis source code learning (31), dictionary learning, dict.c (1)
When QT uses qtooltip mouse to display text, the picture of the button will also be displayed and the prompt text style will be modified
力扣------路径总和 III
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算