当前位置:网站首页>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
}
边栏推荐
- 数学知识(欧拉函数)
- Learn AI safety monitoring project from zero [attach detailed code]
- Record my pytorch installation process and errors
- Cultivate primary and secondary school students' love for educational robots
- C# 图片显示占用问题
- Summary of MySQL key challenges (2)
- Solution: the agent throws an exception error
- June book news | 9 new books are listed, with a strong lineup and eyes closed!
- How to write a client-side technical solution
- How to configure PostgreSQL 12.9 to allow remote connections
猜你喜欢

洛谷入门3【循环结构】题单题解

2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
![[bus interface] Axi interface](/img/ee/95ade7811ec2c37fb67a77f0b6ae2a.jpg)
[bus interface] Axi interface

Video cover image setting, put cover images into multiple videos in the simplest way

Markdown edit syntax

Cubemx DMA notes

Leetcode merge sort linked list

Getting started with pytest -- description of fixture parameters

Video multiple effects production, fade in effect and border background are added at the same time

正大留4的主账户信息汇总
随机推荐
Video cover image setting, put cover images into multiple videos in the simplest way
js面试收藏试题1
6.30 year end summary, end of student age
VMware installation win10 reports an error: operating system not found
Mathematical knowledge -- understanding and examples of fast power
Pit encountered in win11 pytorch GPU installation
fastText文本分类
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
Go Chan's underlying principles
Summary of main account information of zhengdaliu 4
Virtual machine installation deepin system
Practical problem solving ability of steam Education
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
洛谷入门3【循环结构】题单题解
Here comes the chicken soup! Keep this quick guide for data analysts
数据库问题汇总
从数组中找出和为目标的下标
Solution: the agent throws an exception error
Beginner crawler - biqu Pavilion crawler
National all Chinese Automatic Test Software apifox