当前位置:网站首页>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;
}
}
边栏推荐
- CC2530 common registers for ADC single channel conversion
- 网络安全web渗透技术
- function overloading
- PHP production website active push (website)
- 斑马识别成狗,AI犯错的原因被斯坦福找到了
- Redis:关于列表List类型数据的操作命令
- JSON 与 BSON 区别
- What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
- Learn from me about the enterprise flutter project: simplified framework demo reference
- Recommendation of good books on learning QT programming
猜你喜欢

QT serial port UI design and solution to display Chinese garbled code

Idea configuration plug-in

Mysql database -dql

网络安全web渗透技术

Kotlin学习快速入门(7)——扩展的妙用
The way of wisdom (unity of knowledge and action)

arduino-esp32:LVGL项目(一)整体框架

Recommendation of good books on learning QT programming

美团一面:为什么线程崩溃崩溃不会导致 JVM 崩溃

A survey of state of the art on visual slam
随机推荐
What material is sa537cl2? Analysis of mechanical properties of American standard container plate
[mathematical logic] equivalent calculus and reasoning calculus of propositional logic (propositional logic | equivalent calculus | principal conjunctive (disjunctive) paradigm | reasoning calculus)**
[combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
【剑指 Offer】58 - II. 左旋转字符串
A survey of state of the art on visual slam
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
斑马识别成狗,AI犯错的原因被斯坦福找到了
Overview of satellite navigation system
执行脚本不认\r
On Lagrange interpolation and its application
Build your own website (23)
29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
Add color to the interface automation test framework and realize the enterprise wechat test report
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
美团一面:为什么线程崩溃崩溃不会导致 JVM 崩溃
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
Atom QT 16_ audiorecorder