当前位置:网站首页>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;
}
}
边栏推荐
- 【Try to Hack】主动侦查隐藏技术
- [combinatorial mathematics] counting model, common combinatorial numbers and combinatorial identities**
- 什么是质押池,如何进行质押呢?
- UCORE overview
- What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
- [Jianzhi offer] 58 - ii Rotate string left
- 斑马识别成狗,AI犯错的原因被斯坦福找到了
- Deep understanding of grouping sets statements in SQL
- What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
- 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
猜你喜欢
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
Shentong express expects an annual loss of nearly 1billion
Build your own website (23)
Mysql database DDL and DML
The word backspace key cannot delete the selected text, so you can only press Delete
Leetcode: lucky number in matrix
What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
随机推荐
Interpretation of several important concepts of satellite antenna
Difference between JSON and bson
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
ucore概述
CC2530 common registers for port interrupts
Build your own website (23)
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
What is the material of sa302grc? American standard container plate sa302grc chemical composition
The way of wisdom (unity of knowledge and action)
線程池:業務代碼最常用也最容易犯錯的組件
Why is WPA3 security of enterprise business so important?
Unreal_ Datatable implements ID self increment and sets rowname
Kotlin learning quick start (7) -- wonderful use of expansion
Necessary ability of data analysis
Overview of satellite navigation system
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
LeetCode 1656. Design ordered flow
On Lagrange interpolation and its application