当前位置:网站首页>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
}
边栏推荐
- Rhcsa --- work on the fourth day
- AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
- C # picture display occupancy problem
- idea自動導包和自動删包設置
- [common error] the DDR type of FPGA device is selected incorrectly
- Exercise notes 13 (effective letter ectopic words)
- Several methods of capturing packets under CS framework
- Here comes the chicken soup! Keep this quick guide for data analysts
- [quick view opencv] familiar with CV matrix operation with image splicing examples (3)
- 在{{}}中拼接字符
猜你喜欢

Pit encountered in win11 pytorch GPU installation

June book news | 9 new books are listed, with a strong lineup and eyes closed!

Leetcode merge sort linked list

How to write a client-side technical solution

Analyze the space occupied by the table according to segments, clusters and pages

Solution: the agent throws an exception error

Getting started with pytest ----- confitest Application of PY
![[Yu Yue education] autumn 2021 reference materials of Tongji University](/img/50/5136359b89a5d047fe648637643ad0.jpg)
[Yu Yue education] autumn 2021 reference materials of Tongji University

ansible安装与使用

Unity particle Foundation
随机推荐
Pit encountered in win11 pytorch GPU installation
Learn AI safety monitoring project from zero [attach detailed code]
Solution of DM database unable to open graphical interface
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
TypeScript类的使用
idea自动导包和自动删包设置
Mapping settings in elk (8) es
Gin framework learning code
Oracle stored procedure and job task setting
Promise all()
Detailed process of DC-1 range construction and penetration practice (DC range Series)
js面试收藏试题1
Common errors of dmrman offline backup
How to recover deleted data in disk
Here comes the chicken soup! Keep this quick guide for data analysts
解析少儿编程中的动手搭建教程
Leetcode basic programming: array
Exercise notes 13 (effective letter ectopic words)
idea自動導包和自動删包設置
Knowledge arrangement about steam Education