Junedayday Blog

六月天天的个人博客

0%

Go算法实战 - 1.【两数相加LeetCode-2】递归解法

Go-Leetcode

Leetcode-2 两数相加

原题链接 - https://leetcode-cn.com/problems/add-two-numbers/

1
2
3
4
5
6
7
type ListNode struct {
Val int
Next *ListNode
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
}

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
return addTwoNumbersWithCarry(l1, l2, 0)
}

func addTwoNumbersWithCarry(l1 *ListNode, l2 *ListNode, carry int) *ListNode {
var node = new(ListNode)
if l1 != nil {
node.Val += l1.Val
l1 = l1.Next
}

if l2 != nil {
node.Val += l2.Val
l2 = l2.Next
}

node.Val += carry
// 引入位操作
carry, node.Val = node.Val/10, node.Val%10

// 没有后续节点
if l1 == nil && l2 == nil && carry == 0 {
return node
}

node.Next = addTwoNumbersWithCarry(l1, l2, carry)
return node
}

Github: https://github.com/Junedayday/code_reading

Blog: http://junes.tech/

Bilibili: https://space.bilibili.com/293775192

公众号: golangcoding

二维码