# leetcode_445

#### 代码如下：

``````func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
l1 = reverseList(l1)
l2 = reverseList(l2)
return reverseList(temp)
}

//两个链表相加，使用的某大佬的代码
var temp = &ListNode{}
var incr int

lastNode := temp
for {
var v int
if l1 != nil && l2 != nil {
v = l1.Val + l2.Val + incr
if v > 9 {
v = v - 10
incr = 1
} else {
incr = 0
}
l1 = l1.Next
l2 = l2.Next
} else if l1 != nil {
v = l1.Val + incr
if v > 9 {
v = v - 10
incr = 1
} else {
incr = 0
}
l1 = l1.Next
} else if l2 != nil {
v = l2.Val + incr
if v > 9 {
v = v - 10
incr = 1
} else {
incr = 0
}
l2 = l2.Next
} else {
break
}
node := &ListNode{
Val: v,
}
lastNode.Next = node

lastNode = node
}

if incr == 1 {
node := &ListNode{
Val: 1,
}
lastNode.Next = node
}

return temp.Next

}

//翻转链表，用的某位大佬的代码
}

var pre *ListNode = nil
for cur != nil {
tmp := cur.Next
cur.Next = pre
pre = cur
cur = tmp
}
return pre
}
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

#### 代码如下：

``````func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
l1 = reverseList(l1)
l2 = reverseList(l2)
return reverseList(temp)
}

//两个链表相加，使用的某大佬的代码
var temp = &ListNode{}
var incr int

lastNode := temp
for {
var v int
if l1 != nil && l2 != nil {
v = l1.Val + l2.Val + incr
if v > 9 {
v = v - 10
incr = 1
} else {
incr = 0
}
l1 = l1.Next
l2 = l2.Next
} else if l1 != nil {
v = l1.Val + incr
if v > 9 {
v = v - 10
incr = 1
} else {
incr = 0
}
l1 = l1.Next
} else if l2 != nil {
v = l2.Val + incr
if v > 9 {
v = v - 10
incr = 1
} else {
incr = 0
}
l2 = l2.Next
} else {
break
}
node := &ListNode{
Val: v,
}
lastNode.Next = node

lastNode = node
}

if incr == 1 {
node := &ListNode{
Val: 1,
}
lastNode.Next = node
}

return temp.Next

}

//翻转链表，用的某位大佬的代码