当前位置:网站首页>[leetcode] 2. Add two numbers - go language problem solving
[leetcode] 2. Add two numbers - go language problem solving
2022-07-25 01:41:00 【Cabbage that wants to become powerful】
List of articles
One 、 Title Description
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 1:

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
Topic link :https://leetcode.cn/problems/add-two-numbers/
Two 、 Their thinking
The storage order of numbers in the linked list is from front to back : bits , ten , Hundred bit ,…
When two numbers are added , We also start from single digits , If a carry is generated, it goes to ten …
It can be seen that the storage order of the linked list is consistent with the order in which we perform addition operations , We just add from the header , Carry to the next (Next node ) that will do …
The difficulty of this question mainly lies in carry On the disposal , Key points :
- If the result of adding numbers Greater than 9 Will produce carry ,
- When adding, it should be :v1+v2+ carry
- additive Each step May generate carry
3、 ... and 、 My explanation
my Go The language code :
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil && l2 == nil{
return nil
}else if l1 == nil{
return l2
}else if l2 == nil{
return l1
}
c1 := l1 // At present l1 node
b1 := l1 // At present l1 The precursor node of the node
c2 := l2 // At present l2 node
b := 0 // carry
// It's not empty , First deal with the header node
if c1.Val + c2.Val <= 9{
c1.Val = c1.Val + c2.Val
c1 = c1.Next
c2 = c2.Next
}else{
b = (c1.Val + c2.Val)/10
c1.Val = (c1.Val + c2.Val)%10
c1 = c1.Next
c2 = c2.Next
}
// Deal with the following nodes
for c1 != nil && c2 != nil{
if c1.Val + c2.Val + b <= 9{
c1.Val = c1.Val + c2.Val + b
b = 0 // Carry return 0
c1 = c1.Next
b1 = b1.Next
c2 = c2.Next
}else{
newb := (c1.Val + c2.Val + b)/10 // New carry
c1.Val = (c1.Val + c2.Val + b)%10
b = newb
c1 = c1.Next
b1 = b1.Next
c2 = c2.Next
}
}
if b != 0{
// Carry is not empty
if c1 == nil && c2 == nil{
var newnode ListNode
newnode.Val = b
b1.Next = &newnode
return l1
}else if c2 == nil{
for b != 0 {
c1,b = jinwei(c1, b)
c1 = c1.Next
}
return l1
}else if c1 == nil{
t2 := c2
for b != 0 {
c2,b = jinwei(c2,b)
c2 = c2.Next
}
b1.Next = t2
return l1
}
}else{
// Carry is empty , Go straight back to
if c1 == nil && c2 == nil{
return l1
}else if c2 == nil{
return l1
}else if c1 == nil{
b1.Next = c2
return l1
}
}
return l1
}
// Carry function , Carry b Add to the current node , Returns the new value of the current node and the resulting new carry
func jinwei(l1 *ListNode, b int) (*ListNode,int){
if l1.Val + b <= 9{
l1.Val = l1.Val + b
return l1,0
}else{
if l1.Next != nil{
newb := (l1.Val + b)/10
l1.Val = (l1.Val + b)%10
return l1,newb
}else{
newb := (l1.Val + b)/10
l1.Val = (l1.Val + b)%10
var newnode ListNode
newnode.Val = newb
l1.Next = &newnode
return l1,0
}
}
}
Judge the result :
边栏推荐
- Rightmost × Microframe, high quality heif image coding and compression technology
- Kernel structure and design
- Prosci anti-CD22 antibody epratuzum28 flow cytometry display
- [C + + primer notes] Chapter 8 IO Library
- Detailed explanation of zero length array in C language (1) [information at the end of the article]
- Download files and web pages with WGet
- 1260. Two dimensional grid migration: simple construction simulation problem
- Green low-carbon Tianyi cloud, a new engine of digital economy!
- Chip sold at sand price: Lei Jun's dream was "ruined" by this company
- G025-db-gs-ins-02 openeuler deployment opengauss (1 active and 1 standby)
猜你喜欢
![[27. Expression evaluation (infix expression)]](/img/af/cf3c4a441232caeea9f9927ebdf87b.png)
[27. Expression evaluation (infix expression)]
![[linear DP] Digital triangle](/img/8c/f7392733ef8e13b20c394da8b4c403.png)
[linear DP] Digital triangle

On let variable promotion

Start to build a three node Eureka cluster

Hcip - BGP - border gateway protocol

Promise basic use

Windows security hardening -- close unnecessary ports
![Detailed explanation of zero length array in C language (1) [information at the end of the article]](/img/89/1f01e24ce52b2d459f26397cd8527f.png)
Detailed explanation of zero length array in C language (1) [information at the end of the article]

Custom type

Amd epyc 9654 Genoa CPU cache test exposure L1 bandwidth up to 30tb/s
随机推荐
Several application scenarios of NAT
Academicians said: researchers should also support their families. They can only do short-term and fast research if they are not promoted
Create the first hbuilder project
Amd epyc 9654 Genoa CPU cache test exposure L1 bandwidth up to 30tb/s
Sort out some scattered knowledge points by yourself
How to implement the server anti blackmail virus system is a problem we have to consider
How to implement a state machine?
Hbuilderx developed by uni app connects to night God simulator
Ad active directory and domain network
C traps and defects Chapter 2 lexical "traps" 2.4 switch statements
Rightmost × Microframe, high quality heif image coding and compression technology
Visual studio code installation package download slow & Installation & environment configuration & new one-stop explanation
iptables :chains, target
Nacos hand to hand teaching [i] dynamic configuration of Nacos
[summer daily question] Luogu p1706 full ranking question
Point to point copy and paste of web pages
10 commonly used data visualization tool software
Kubernetes creates a user with dashboard read-only permission (with exec permission)
The solution of displaying garbled code in SecureCRT
The most complete summary of MySQL data types in history - (first)