/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/funcaddTwoNumbers(l1*ListNode,l2*ListNode)*ListNode{varreversefunc(root*ListNode)*ListNodereverse=func(root*ListNode)*ListNode{ifnil==root||root.Next==nil{returnroot}newRoot:=reverse(root.Next)root.Next.Next=rootroot.Next=nilreturnnewRoot}l1,l2=reverse(l1),reverse(l2)dummy:=&ListNode{}carry,node:=0,dummyforl1!=nil||l2!=nil||carry!=0{ifl1!=nil{carry+=l1.Vall1=l1.Next}ifl2!=nil{carry+=l2.Vall2=l2.Next}node.Next=&ListNode{Val:carry%10}node=node.Nextcarry/=10}returnreverse(dummy.Next)}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/funcaddTwoNumbers(l1*ListNode,l2*ListNode)*ListNode{varstk1,stk2[]intforl1!=nil{stk1=append(stk1,l1.Val)l1=l1.Next}forl2!=nil{stk2=append(stk2,l2.Val)l2=l2.Next}varprev*ListNodecarry:=0forlen(stk1)>0||len(stk2)>0||carry!=0{iflen(stk1)>0{carry+=stk1[len(stk1)-1]stk1=stk1[:len(stk1)-1]}iflen(stk2)>0{carry+=stk2[len(stk2)-1]stk2=stk2[:len(stk2)-1]}prev=&ListNode{Val:carry%10,Next:prev}carry/=10}returnprev}