当前位置:网站首页>[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 :
边栏推荐
- [28. Maximum XOR pair]
- What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?
- The current situation of the industry is disappointing. After working, I returned to UC Berkeley to study for a doctoral degree
- Several schemes of traffic exposure in kubernetes cluster
- WhatsApp web for usability testing of software testing technology
- If in ython__ name__ == ‘__ main__‘: Function and principle of
- Example analysis of recombinant monoclonal antibody prosci CD154 antibody
- MySQL Basics (concepts, common instructions)
- Ecosystem long-term observation data product system
- Kernel structure and design
猜你喜欢
![[programmer interview classic] 01.09 string rotation](/img/d2/7ea9351c31af01665d86f8c6bc3468.png)
[programmer interview classic] 01.09 string rotation

What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?

Top priority of dry goods: common indicators and terms in data analysis!

Start to build a three node Eureka cluster

7.18 - daily question - 408

Rightmost × Microframe, high quality heif image coding and compression technology

Pychart exits pytest mode (run pytest in mode)
10 commonly used data visualization tool software

Why is the exclude or include attribute setting of the < keep alive > component invalid

Service address dynamic awareness of Nacos registry
随机推荐
Commonjs export import
POM reports an error
[28. Maximum XOR pair]
Interpretation of video generation paper of fed shot video to video (neurips 2019)
The position of the nth occurrence of MySQL in the string
Performance analysis method - Notes on top of performance
Resolution of multi thread conflict lock
About the difference between for... In and for... Of and object. Keys()
Service address dynamic awareness of Nacos registry
Eolink - quickly develop interfaces through document driven
The two supply chain centers of HEMA launched the "background" of innovative research and development of multi format commodities
[summer daily question] output of Luogu p1157 combination
7.20 - daily question - 408
The IPO of Tuba rabbit was terminated: the annual profit fell by 33%, and Jingwei Sequoia was the shareholder
7.18 - daily question - 408
Prosci 14-3-3 (phosphate ser58) antibody instructions
[daily question in summer] Luogu p6850 Noi
2022.7.20 linear table
Open source demo | release of open source example of arcall applet
The cloud ecology conference comes with the "peak"!