当前位置:网站首页>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;
}
}
边栏推荐
- 图之深度优先搜索
- MySQL converts comma separated attribute field data from column to row
- C语言字符串练习
- Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
- Network security web penetration technology
- 【剑指 Offer】58 - II. 左旋转字符串
- [combinatorics] recursive equation (example 1 of recursive equation | list recursive equation)
- visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
- C语言字符串反转
- Deep understanding of grouping sets statements in SQL
猜你喜欢

Static program analysis (I) -- Outline mind map and content introduction

Processing strategy of message queue message loss and repeated message sending

Talk about several methods of interface optimization

29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
智慧之道(知行合一)

Aike AI frontier promotion (7.3)

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

Shentong express expects an annual loss of nearly 1billion

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

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
随机推荐
[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
网络安全web渗透技术
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
Analysis of variance summary
MySQL single table field duplicate data takes the latest SQL statement
Static program analysis (I) -- Outline mind map and content introduction
C语言字符串练习
[mathematical logic] equivalent calculus and reasoning calculus of propositional logic (propositional logic | equivalent calculus | principal conjunctive (disjunctive) paradigm | reasoning calculus)**
Simulink oscilloscope data is imported into Matlab and drawn
线程池:业务代码最常用也最容易犯错的组件
建立自己的网站(23)
[combinatorics] non descending path problem (number of non descending paths with constraints)
Recommendation of good books on learning QT programming
IL Runtime
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Execute script unrecognized \r
Fast Ethernet and Gigabit Ethernet: what's the difference?
[combinatorial mathematics] recursive equation (example of recursive equation 2 Hanoi Tower | example of recursive equation 3 insertion sequencing)
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
定义一个结构体Fraction,表示分数,用于表示 2/3, 5/6这样的分数