-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.两数相加.go
66 lines (57 loc) · 891 Bytes
/
2.两数相加.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* @lc app=leetcode.cn id=2 lang=golang
*
* [2] 两数相加
*/
// @lc code=start
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var (
head, tail *ListNode
carry = 0
)
for l1 != nil || l2 != nil {
n1, n2 := 0, 0
if l1 != nil {
n1 = l1.Val
// 移动l1
l1 = l1.Next
}
if l2 != nil {
n2 = l2.Val
// 移动l2
l2 = l2.Next
}
sum := n1 + n2 + carry
left := sum % 10
carry = sum / 10
if head == nil {
// 初始化tail, head
head = &ListNode{
Val: left,
}
tail = head
} else {
node := &ListNode{
Val: left,
}
// 移动tail
tail.Next = node
tail = tail.Next
}
}
if carry > 0 {
node := &ListNode{
Val: carry,
}
tail.Next = node
}
return head
}
// @lc code=end