当前位置:网站首页>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 .
边栏推荐
- [function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram
- 【一起上水硕系列】Day 7 内容+Day8
- [untitled]
- Why is the website slow to open?
- [teacher Zhao Yuqiang] use Oracle's tracking file
- @Import annotation: four ways to import configuration classes & source code analysis
- How to create and configure ZABBIX
- Troubleshooting of 32GB Jetson Orin SOM failure to brush
- redis 遇到 NOAUTH Authentication required
- 为什么网站打开速度慢?
猜你喜欢
![[teacher Zhao Yuqiang] index in mongodb (Part 1)](/img/2d/277ec737f2a7065831a19d036e61e1.jpg)
[teacher Zhao Yuqiang] index in mongodb (Part 1)
![[teacher Zhao Yuqiang] RDB persistence of redis](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] RDB persistence of redis

pytorch DataLoader实现miniBatch(未完成)
![[teacher Zhao Yuqiang] MySQL flashback](/img/93/75998e28fd309880661ea723dc8de6.jpg)
[teacher Zhao Yuqiang] MySQL flashback
![[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation](/img/c2/991b8febd262cf9237017adc9d1221.jpg)
[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation

Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow

Kubernetes cluster environment construction & Deployment dashboard

Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading

Synthetic keyword and NBAC mechanism

深度学习,从一维特性输入到多维特征输入引发的思考
随机推荐
Use telnet to check whether the port corresponding to the IP is open
【一起上水硕系列】Day 7 内容+Day8
Final review (Day2)
How does win7 solve the problem that telnet is not an internal or external command
Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow
伯努利分布,二项分布和泊松分布以及最大似然之间的关系(未完成)
The server data is all gone! Thinking caused by a RAID5 crash
Personal outlook | looking forward to the future from Xiaobai's self analysis and future planning
期末复习(day3)
Solve the problem of automatic disconnection of SecureCRT timeout connection
[teacher Zhao Yuqiang] kubernetes' probe
【无标题】
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
Redis cannot connect remotely.
88. Merge two ordered arrays
JS implements the problem of closing the current child window and refreshing the parent window
ansible防火墙firewalld设置
88. 合并两个有序数组
期末复习(Day5)
[teacher Zhao Yuqiang] use Oracle's tracking file