当前位置:网站首页>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
}
边栏推荐
- Mathematical knowledge -- understanding and examples of fast power
- 2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
- 案例分享|智慧化的西部机场
- 6.30年终小结,学生时代结束
- [bus interface] Axi interface
- The underlying principle of go map (storage and capacity expansion)
- How to modify data file path in DM database
- Leetcode- insert and sort the linked list
- June book news | 9 new books are listed, with a strong lineup and eyes closed!
- Beginner crawler - biqu Pavilion crawler
猜你喜欢
Embedded-c language-8-character pointer array / large program implementation
Practical problem solving ability of steam Education
Orthogonal test method and function diagram method for test case design
面试会问的 Promise.all()
Solution of DM database unable to open graphical interface
[Yu Yue education] autumn 2021 reference materials of Tongji University
How to recover deleted data in disk
How to modify data file path in DM database
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
数学知识——快速幂的理解及例题
随机推荐
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
Design and implementation of general interface open platform - (44) log processing of API services
國產全中文-自動化測試軟件Apifox
Starting from the classification of database, I understand the map database
Win10 disk management compressed volume cannot be started
Practical problem solving ability of steam Education
Summary of database problems
Getting started with pytest -- description of fixture parameters
Feign realizes file uploading and downloading
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Embedded-c language-9-makefile/ structure / Consortium
DMA Porter
LM09丨费雪逆变换反转网格策略
Getting started with pytest ----- confitest Application of PY
Fasttext text text classification
C # picture display occupancy problem
How to modify data file path in DM database
正大美欧4的主账户关注什么数据?
面试会问的 Promise.all()
Application d'un robot intelligent dans le domaine de l'agroécologie