/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var curr1 *ListNode = l1
var curr2 *ListNode = l2
var res = &ListNode{}
var curr *ListNode = res
var add int
for add!=0 || curr1!=nil || curr2!=nil{
var x, y int
if curr1!=nil{
x = curr1.Val
curr1 = curr1.Next
}
if curr2!=nil{
y = curr2.Val
curr2 = curr2.Next
}
var result = x + y + add
add = result/10
newNode := &ListNode{Val:result%10}
curr.Next = newNode
curr = newNode
}
return res.Next
}
原文:https://www.cnblogs.com/pangqianjin/p/14657329.html