当前位置:网站首页>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
}
边栏推荐
- Fasttext text text classification
- 解析少儿编程中的动手搭建教程
- AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
- C# 基于MQTTNet的服务端与客户端通信案例
- Learn AI safety monitoring project from zero [attach detailed code]
- 【ClickHouse】How to create index for Map Type Column or one key of it?
- DMA Porter
- MySQL table insert Chinese change? Solution to the problem of No
- 06 装饰(Decorator)模式
- Go Chan's underlying principles
猜你喜欢

CubeMx DMA笔记

Gin framework learning code

Change deepin to Alibaba image source

One step implementation of yolox helmet detection (combined with oak intelligent depth camera)

How to configure PostgreSQL 12.9 to allow remote connections

Virtual machine installation deepin system

Summary of common string processing functions in C language

解决:代理抛出异常错误

Common errors of dmrman offline backup

About PROFIBUS: communication backbone network of production plant
随机推荐
Mathematical knowledge (Euler function)
oracle 存储过程与job任务设置
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
[bus interface] Axi interface
Markdown edit syntax
Steam教育的实际问题解决能力
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
Solution: the agent throws an exception error
Use of typescript classes
Change deepin to Alibaba image source
Summary of main account information of zhengdaliu 4
Lay the foundation for children's programming to become a basic discipline
Promise all()
The underlying principle of go map (storage and capacity expansion)
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
Mouse events in JS
Hcip day 17
Solution of DM database unable to open graphical interface
Ruby replaces gem Alibaba image
Orthogonal test method and function diagram method for test case design