当前位置:网站首页>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 .
边栏推荐
- [video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
- Detailed explanation of contextclassloader
- How to create and configure ZABBIX
- [advanced pointer (1)] | detailed explanation of character pointer, pointer array, array pointer
- Sorry, this user does not exist!
- [function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
- Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
- Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
- Apache+PHP+MySQL环境搭建超详细!!!
- Sophomore dilemma (resumption)
猜你喜欢
![[teacher Zhao Yuqiang] Cassandra foundation of NoSQL database](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] Cassandra foundation of NoSQL database

Final review (Day5)

Clickhouse learning notes (2): execution plan, table creation optimization, syntax optimization rules, query optimization, data consistency
![[trivia of two-dimensional array application] | [simple version] [detailed steps + code]](/img/84/98c1220d0f7bc3a948125ead6ff3d9.jpg)
[trivia of two-dimensional array application] | [simple version] [detailed steps + code]

Mapbox tasting value cloud animation
![[teacher Zhao Yuqiang] index in mongodb (Part 2)](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[teacher Zhao Yuqiang] index in mongodb (Part 2)

JDBC connection database steps

Qt读写Excel--QXlsx插入图表5

pytorch DataLoader实现miniBatch(未完成)

2022.DAY592
随机推荐
chromedriver对应版本下载
88. Merge two ordered arrays
【无标题】
Today, many CTOs were killed because they didn't achieve business
AtCoder Beginner Contest 258(A-D)
Es 2022 officially released! What are the new features?
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation
mapbox尝鲜值之云图动画
kubernetes资源对象介绍及常用命令(五)-(ConfigMap)
BeanDefinitionRegistryPostProcessor
Alibaba cloud Alipay sandbox payment
MySQL 5.7.32-winx64 installation tutorial (support installing multiple MySQL services on one host)
How to create your own repository for software packages on Debian
How does win7 solve the problem that telnet is not an internal or external command
[escape character] [full of dry goods] super detailed explanation + code illustration!
Introduction to redis using Lua script
大二困局(复盘)
[teacher Zhao Yuqiang] MySQL flashback
88. 合并两个有序数组
Using the ethtool command by example