当前位置:网站首页>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
}
边栏推荐
- The underlying principle of go map (storage and capacity expansion)
- 数学知识(欧拉函数)
- Record the bug of unity 2020.3.31f1 once
- Practical problem solving ability of steam Education
- 06 decorator mode
- Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
- Go Chan's underlying principles
- DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
- Domestic all Chinese automatic test software apifox
- Application of intelligent robot in agricultural ecology
猜你喜欢

Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?

2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路

Ansible installation and use

将光盘中的cda保存到电脑中

Mathematical knowledge (Euler function)

Record my pytorch installation process and errors

Typescript function details

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

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

TypeScript函数详解
随机推荐
Federal learning: dividing non IID samples according to Dirichlet distribution
C case of communication between server and client based on mqttnet
TypeScript函数详解
06 decorator mode
MySQL table insert Chinese change? Solution to the problem of No
The underlying principle of go map (storage and capacity expansion)
idea自動導包和自動删包設置
Basic differences between Oracle and MySQL (entry level)
6.30 year end summary, end of student age
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
Use of Baidu map
Embedded-c language-9-makefile/ structure / Consortium
Comp 250 parsing
Mathematical knowledge -- understanding and examples of fast power
Rhcsa --- work on the fourth day
Go Chan's underlying principles
win10 磁盘管理 压缩卷 无法启动问题
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
UNET deployment based on deepstream