Ai
20 Star 8 Fork 8

爱因诗贤/Leetcode

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
addTwoNumbers.go 1.43 KB
Copy Edit Raw Blame History
qinshixian authored 2021-06-10 00:23 +08:00 . 外观数列go
package main
/**
* 2. 两数相加
*给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
*
* 请你将两个数相加,并以相同形式返回一个表示和的链表。
*
* 你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
*
* 示例:
* 输入:
* 2->4->3->null
* 5->6->4->null
* 输出:
* 7->0->8->null
*/
/**
* 思路1:将链表转换为整数,然后相加之后的数再转换为链表
* 思路2:依次遍历两个链表,然后按位求和,将求和的结果插入到新链表尾部,有进位的标记 flag,计入到下一位求和,最后得到新链表
*/
type ListNode struct {
Val int
Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var head, tail *ListNode
flag := 0
for l1 != nil || l2 != nil {
num1, num2, sum := 0, 0, 0
if l1 != nil {
num1 = l1.Val
}
if l2 != nil {
num2 = l2.Val
}
sum = num1 + num2 + flag
flag = sum / 10
if head == nil {
head = &ListNode{Val: sum % 10, Next: nil}
tail = &ListNode{Val: sum % 10, Next: nil}
} else {
tail.Next = &ListNode{Val: sum % 10, Next: nil}
tail = tail.Next
}
if l1 != nil {
l1 = l1.Next
}
if l2 != nil {
l2 = l2.Next
}
}
if flag > 0 {
tail.Next = &ListNode{Val: flag, Next: nil}
}
return head
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/love-for-poetry/Leetcode.git
git@gitee.com:love-for-poetry/Leetcode.git
love-for-poetry
Leetcode
Leetcode
love-for-poetry

Search