Golang:
思路:组合题,先反转链表,再让它们相加,再反转回来即可。针对题目提到的进阶,可以先将两个链表拷贝下来做大数加法,再赋值回去或者新开一条链表
代码如下:
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
l1 = reverseList(l1)
l2 = reverseList(l2)
temp:=addTwoLinkedLists(l1,l2)
return reverseList(temp)
}
//两个链表相加,使用的某大佬的代码
func addTwoLinkedLists(l1 *ListNode, l2 *ListNode) *ListNode {
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
}
//翻转链表,用的某位大佬的代码
func reverseList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
var pre *ListNode = nil
var cur *ListNode = head
for cur != nil {
tmp := cur.Next
cur.Next = pre
pre = cur
cur = tmp
}
return pre
}
有疑问加站长微信联系(非本文作者)