LeetCode 2. 两数相加
题目描述
🔥 2. 两数相加
思路分析
加法问题
参考代码
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
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
} else if l2 == nil {
return l1
}
dummy := &ListNode{}
cur := dummy
carry := 0
for l1 != nil || l2 != nil || carry > 0 {
total := carry
if l1 != nil {
total += l1.Val
l1 = l1.Next
}
if l2 != nil {
total += l2.Val
l2 = l2.Next
}
cur.Next = &ListNode{Val: total % 10}
cur = cur.Next
carry = total / 10
}
return dummy.Next
}
1
write your code here
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用