当前位置:网站首页>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
}
边栏推荐
- [common error] the DDR type of FPGA device is selected incorrectly
- Super detailed pycharm tutorial
- CubeMx DMA笔记
- Getting started with pytest -- description of fixture parameters
- 2022-003arts: recursive routine of binary tree
- 6.30年终小结,学生时代结束
- idea自動導包和自動删包設置
- How to modify data file path in DM database
- Summary of common string processing functions in C language
- Hcip day 17
猜你喜欢
Learn BeanShell before you dare to say you know JMeter
Vmware安装win10报错:operating system not found
Mapping location after kotlin confusion
正大留4的主账户信息汇总
idea自動導包和自動删包設置
Summary of main account information of zhengdaliu 4
How to recover deleted data in disk
Typescript function details
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
解决:代理抛出异常错误
随机推荐
C - derived classes and constructors
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
What data does the main account of Zhengda Meiou 4 pay attention to?
Embedded-c language-8-character pointer array / large program implementation
设置滚动条默认样式 谷歌浏览器
How to configure PostgreSQL 12.9 to allow remote connections
Super detailed pycharm tutorial
Cannot activate CONDA virtual environment in vscode
[opencv] image binarization
Pyflink writes MySQL examples with JDBC
Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
解析少儿编程中的动手搭建教程
js面试收藏试题1
Realize the function of data uploading
I sorted out some basic questions about opencv AI kit.
idea自動導包和自動删包設置
CubeMx DMA笔记
Oracle和MySQL的基本区别(入门级)