当前位置:网站首页>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
}
边栏推荐
- A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
- Gin framework learning code
- Markdown edit syntax
- 删除排序数组中的重复项go语言实现
- UNET deployment based on deepstream
- Change deepin to Alibaba image source
- Mapping settings in elk (8) es
- How to modify data file path in DM database
- Mathematical knowledge -- understanding and examples of fast power
- 【ClickHouse】How to create index for Map Type Column or one key of it?
猜你喜欢

How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer

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

Let genuine SMS pressure measurement open source code

Analyzing the hands-on building tutorial in children's programming
![Introduction to Luogu 3 [circular structure] problem list solution](/img/fd/c0c5687c7e6e74bd5c911b27c3e19c.png)
Introduction to Luogu 3 [circular structure] problem list solution

Mysql database learning

DMA Porter

Online incremental migration of DM database

Vmware安装win10报错:operating system not found

数学知识——快速幂的理解及例题
随机推荐
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Mapping location after kotlin confusion
Realize the function of data uploading
I sorted out some basic questions about opencv AI kit.
Cultivate primary and secondary school students' love for educational robots
Domestic all Chinese automatic test software apifox
JS interview collection test question 1
idea自動導包和自動删包設置
C# 基于MQTTNet的服务端与客户端通信案例
Super detailed pycharm tutorial
ansible安装与使用
Feign realizes file uploading and downloading
TypeScript函数详解
Learn BeanShell before you dare to say you know JMeter
Several methods of capturing packets under CS framework
解析少儿编程中的动手搭建教程
Leetcode merge sort linked list
6.30年终小结,学生时代结束
Basic differences between Oracle and MySQL (entry level)
el-cascader回显只选中不显示的问题