当前位置:网站首页>One brush 145 force deduction hot question-2 sum of two numbers (m)
One brush 145 force deduction hot question-2 sum of two numbers (m)
2022-07-03 16:57:00 【Tang and Song Dynasties】
subject :
Here are two for you Non empty The linked list of , Represents two nonnegative integers .
Each of them is based on The reverse Stored in , And each node can only store a Numbers .
Please add up the two numbers , And returns a linked list representing sum in the same form .
You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
--------------------
Example :

Input :l1 = [2,4,3], l2 = [5,6,4]
Output :[7,0,8]
explain :342 + 465 = 807.
Example 2:
Input :l1 = [0], l2 = [0]
Output :[0]
Example 3:
Input :l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output :[8,9,9,9,0,0,0,1]
Tips :
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
--------------------
reflection :
For the list problem , When the return result is the header node , Usually you need to initialize a pre pointer first pre,
The next node of the pointer points to the real header node head. The purpose of using the pre pointer is that there is no node value available when the list is initialized ,
And the construction process of linked list needs pointer movement , This will result in the loss of the head pointer , Unable to return a result .
label : Linked list
Think of two linked lists as traversal of the same length , If a link list is short, fill it in the front 00,
such as 987 + 23 = 987 + 023 = 1010
Each bit needs to consider the carry problem of the previous bit at the same time , The carry value needs to be updated after the current bit calculation
If the two lists are all traversed , Carry value is 11, Then add the node at the front of the new list 11
/** 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;} } */
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//res Store results ,cur by res The tail pointer of
ListNode res = new ListNode();
ListNode cur = res;
// It means carry
int carry = 0;
while (l1 != null || l2 != null){
// If one of them reaches the end , Then the number of this digit in the linked list is 0.
int a = l1 == null ? 0 : l1.val;
int b = l2 == null ? 0 : l2.val;
// The two digits of the two linked lists are added
int sum = a + b + carry;
// Greater than 10 carry
carry = sum / 10;
// The remainder after rounding
sum %= 10;
// Create an access node res Back
cur.next = new ListNode(sum);
cur = cur.next;
// Move back... Respectively
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
}
// If there is a carry at the end , Add a node
if (carry == 1) cur.next = new ListNode(1);
return res.next;
}
}
边栏推荐
- [2. Basics of Delphi grammar] 2 Object Pascal data type
- Necessary ability of data analysis
- 29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
- NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
- UCORE overview
- Résolution de l'instance d'assemblage - - affichage à l'écran en mode réel
- Thread pool: the most common and error prone component of business code
- RF analyze demo build step by step
- What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
- ucore概述
猜你喜欢
![[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe

2022.02.14_ Daily question leetcode five hundred and forty

ANOVA example

什么是质押池,如何进行质押呢?

Thread pool executes scheduled tasks

Netease UI automation test exploration: airtest+poco

CC2530 common registers for crystal oscillator settings

Daily code 300 lines learning notes day 10

静态程序分析(一)—— 大纲思维导图与内容介绍

What material is sa537cl1? Sa537cl1 corresponds to the national standard material
随机推荐
LeetCode 1658. Minimum operand to reduce x to 0
[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
Recommendation of good books on learning QT programming
RF analyze demo build step by step
Analysis of variance summary
[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
匯編實例解析--實模式下屏幕顯示
PHP production website active push (website)
Aike AI frontier promotion (7.3)
Assembly instance analysis -- screen display in real mode
27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
执行脚本不认\r
跨境电商:外贸企业做海外社媒营销的优势
[try to hack] active detection and concealment technology
29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
Shentong express expects an annual loss of nearly 1billion
[sword finger offer] 58 - I. flip the word order
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
CC2530 common registers for port interrupts