当前位置:网站首页>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;
}
}
边栏推荐
- Network security web penetration technology
- 深入理解 SQL 中的 Grouping Sets 语句
- NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri
- How to judge the region of an IP through C?
- IL Runtime
- 消息队列消息丢失和消息重复发送的处理策略
- On Lagrange interpolation and its application
- Assembly instance analysis -- screen display in real mode
- arduino-esp32:LVGL项目(一)整体框架
- What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
猜你喜欢
美团一面:为什么线程崩溃崩溃不会导致 JVM 崩溃
智慧之道(知行合一)
Depth first search of graph
What is the material of sa302grc? American standard container plate sa302grc chemical composition
Kotlin learning quick start (7) -- wonderful use of expansion
What is the pledge pool and how to pledge?
Daily code 300 lines learning notes day 10
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
Build your own website (23)
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
随机推荐
What is the maximum number of concurrent TCP connections for a server? 65535?
QT serial port UI design and solution to display Chinese garbled code
AcWing 第58 场周赛
Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard
Interpretation of several important concepts of satellite antenna
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
The way of wisdom (unity of knowledge and action)
Take you to API development by hand
[Jianzhi offer] 58 - ii Rotate string left
MySQL single table field duplicate data takes the latest SQL statement
word 退格键删除不了选中文本,只能按delete
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
汇编实例解析--实模式下屏幕显示
How to promote cross department project collaboration | community essay solicitation
跨境电商:外贸企业做海外社媒营销的优势
[mathematical logic] equivalent calculus and reasoning calculus of propositional logic (propositional logic | equivalent calculus | principal conjunctive (disjunctive) paradigm | reasoning calculus)**
LeetCode 1657. Determine whether the two strings are close
Build your own website (23)
【剑指 Offer】58 - I. 翻转单词顺序
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time