当前位置:网站首页>Implementation of leetcode two number addition go
Implementation of leetcode two number addition go
2022-07-02 05:00:00 【Jimmy_ jimi】
The goal is : Addition of two numbers
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 .
Result output :
Here we mainly study the linked list operation and the carry of the last bit, which need to be considered carefully .
func addTwoNumbers(l1, l2 *ListNode) (head *ListNode) {
var tail *ListNode
carry := 0
for l1 != nil || l2 != nil {
n1, n2 := 0, 0
if l1 != nil {
n1 = l1.Val
l1 = l1.Next
}
if l2 != nil {
n2 = l2.Val
l2 = l2.Next
}
sum := n1 + n2 + carry
sum, carry = sum%10, sum/10
if head == nil {
// Create a node head
head = &ListNode{
Val: sum}
// The position where the head node is equal to the tail node
tail = head
} else {
// The tail pointer points to the position of the new node
tail.Next = &ListNode{
Val: sum}
// Tail node pointed to by tail pointer
tail = tail.Next
}
}
if carry > 0 {
tail.Next = &ListNode{
Val: carry}
}
return
}
边栏推荐
- Win10 disk management compressed volume cannot be started
- Introduction to Luogu 3 [circular structure] problem list solution
- 06 decorator mode
- Mapping settings in elk (8) es
- Oracle和MySQL的基本区别(入门级)
- Typescript function details
- Leetcode- insert and sort the linked list
- Save the CDA from the disc to the computer
- Comp 250 parsing
- idea自动导包和自动删包设置
猜你喜欢

The underlying principle of go map (storage and capacity expansion)

Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I

Rhcsa --- work on the third day

Lay the foundation for children's programming to become a basic discipline
![Learn AI safety monitoring project from zero [attach detailed code]](/img/a9/cb93f349229e86cbb05ad196ae9553.jpg)
Learn AI safety monitoring project from zero [attach detailed code]

解决:代理抛出异常错误

数学知识(欧拉函数)

数学问题(数论)试除法做质数的判断、分解质因数,筛质数

正大美欧4的主账户关注什么数据?

Rhcsa --- work on the fourth day
随机推荐
VMware installation win10 reports an error: operating system not found
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
How to modify data file path in DM database
Preparation for writing SAP ui5 applications using typescript
Exercise notes 13 (effective letter ectopic words)
Solution: the agent throws an exception error
Cubemx DMA notes
Practical problem solving ability of steam Education
Summary of MySQL key challenges (2)
[high speed bus] Introduction to jesd204b
面试会问的 Promise.all()
el-cascader回显只选中不显示的问题
Mysql重点难题(2)汇总
UNET deployment based on deepstream
Comp 250 parsing
How to recover deleted data in disk
Cultivate primary and secondary school students' love for educational robots
Hcip day 17