当前位置:网站首页>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
}
边栏推荐
- Realize the function of data uploading
- GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
- C - derived classes and constructors
- Ansible installation and use
- Federal learning: dividing non IID samples according to Dirichlet distribution
- How to recover deleted data in disk
- Introduction to Luogu 3 [circular structure] problem list solution
- Simple and practical accounting software, so that accounts can be checked
- 数学知识——快速幂的理解及例题
- Comp 250 parsing
猜你喜欢

Analyze the space occupied by the table according to segments, clusters and pages

Summary of common string processing functions in C language

Win10 disk management compressed volume cannot be started

Mathematical knowledge -- understanding and examples of fast power

06 装饰(Decorator)模式

2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas

Change deepin to Alibaba image source

Application d'un robot intelligent dans le domaine de l'agroécologie

培养中小学生对教育机器人的热爱之心

Steam教育的实际问题解决能力
随机推荐
National all Chinese Automatic Test Software apifox
Gin framework learning code
Summary of main account information of zhengdaliu 4
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
将光盘中的cda保存到电脑中
Vmware安装win10报错:operating system not found
農業生態領域智能機器人的應用
从数组中找出和为目标的下标
Realize the function of data uploading
Save the CDA from the disc to the computer
Virtual machine installation deepin system
Mysql database learning
Mathematical knowledge (Euler function)
Markdown edit syntax
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
[opencv] image binarization
面试会问的 Promise.all()
VMware installation win10 reports an error: operating system not found
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of