当前位置:网站首页>Leetcode solution - 02 Add Two Numbers
Leetcode solution - 02 Add Two Numbers
2022-07-03 05:58:00 【Ashley shot the sun】
Leetcode The first 02.Add Two Numbers topic , questions Medium.
One . Subject requirements
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
The main idea of the topic
Two integers are stored in two linked lists in reverse order . That is, each node of the linked list stores a digit , The first node stores bits , And so on . Sum through these two linked lists .
Two . Their thinking & Code implementation
Since it's summation , Then it's OK to traverse the two linked lists from scratch and add them , Two problems need to be paid attention to in the process of traversal calculation :
Carry problem in calculation , In addition, if there is still carry after traversal, you need to create a new node storage
There is a linked list traversal completed in advance , Then the value of subsequent traversal of the linked list is 0
Although it is medium Difficult questions , But it's not difficult to do , Just think about the calculation in the traversal process . The implementation code is as follows :
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode result = new ListNode(0);
ListNode tail = result;
// Carry or not
int isopsephy = 0;
while (l1 != null || l2 != null) {
int value1 = l1 == null ? 0:l1.val;
int value2 = l2 == null ? 0:l2.val;
int val = value1 + value2 + isopsephy;
if (val > 9) {
val = val % 10;
isopsephy = 1;
}else {
isopsephy = 0;
}
tail.val = val;
l1 = l1 == null? null: l1.next;
l2 = l2 == null? null: l2.next;
// Jingle
if (l1 == null && l2 == null) {
if (isopsephy > 0) {
tail.next = new ListNode(isopsephy);
tail = tail.next;
}
break;
}
tail.next = new ListNode(0);
tail = tail.next;
}
return result;
}
}
- Running results
Runtime: 1 ms, faster than 100.00% of Java online submissions for Add Two Numbers.
Lao tie saw this and gave a wave of praise 、 Comment on 、 How about paying attention to Sanlian
I am a AhriJ Zou classmate , Front and rear ends 、 Applet 、DevOps Are engaged in the explosion of stack engineers . The blog is constantly updated , If you think it's good , Welcome to the old tiesanlian , If it's not good, you're welcome to correct , learn from each other , Common progress .
边栏推荐
- mapbox尝鲜值之云图动画
- Clickhouse learning notes (2): execution plan, table creation optimization, syntax optimization rules, query optimization, data consistency
- How to create your own repository for software packages on Debian
- 2022.7.2day594
- Solve the problem of automatic disconnection of SecureCRT timeout connection
- [teacher Zhao Yuqiang] use Oracle's tracking file
- redis 无法远程连接问题。
- Ansible firewall firewalld setting
- 理解 YOLOV1 第一篇 预测阶段
- 一起上水硕系列】Day 9
猜你喜欢
Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
Synthetic keyword and NBAC mechanism
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis
最大似然估计,散度,交叉熵
项目总结--04
[teacher Zhao Yuqiang] use the catalog database of Oracle
Detailed explanation of contextclassloader
Alibaba cloud OOS file upload
深度学习,从一维特性输入到多维特征输入引发的思考
随机推荐
The programmer shell with a monthly salary of more than 10000 becomes a grammar skill for secondary school. Do you often use it!!!
It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
Why should there be a firewall? This time xiaowai has something to say!!!
Jedis source code analysis (I): jedis introduction, jedis module source code analysis
Life is a process of continuous learning
Ext4 vs XFS -- which file system should you use
[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
Code generator - single table query crud - generator
[teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
[teacher Zhao Yuqiang] Flink's dataset operator
[teacher Zhao Yuqiang] use Oracle's tracking file
88. 合并两个有序数组
Redhat7系统root用户密码破解
[untitled]
redis 遇到 NOAUTH Authentication required
Convolution operation in convolution neural network CNN
2022.7.2day594
CAD插件的安裝和自動加載dll、arx
【一起上水硕系列】Day 7 内容+Day8
How to create and configure ZABBIX