forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd_Two_Numbers.go
47 lines (40 loc) · 848 Bytes
/
Add_Two_Numbers.go
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
)
type ListNode struct {
Val int
Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
ret := new(ListNode)
cur := ret
add := 0
for l1 != nil || l2 != nil || add > 0 {
val1, val2 := 0, 0
if l1 != nil {
val1,l1 = l1.Val,l1.Next
}
if l2 != nil {
val2,l2 = l2.Val,l2.Next
}
val := val1 + val2 + add
add = val / 10
cur.Next = &ListNode{Val:val%10, Next:nil}
cur = cur.Next
}
return ret.Next
}
func main() {
l1 := ListNode{Val:0, Next:nil}
l1.Next = &ListNode{Val:4, Next:nil}
l1.Next.Next = &ListNode{Val:3, Next:nil}
l2 := ListNode{Val:0, Next:nil}
l2.Next = &ListNode{Val:6, Next:nil}
l2.Next.Next = &ListNode{Val:4, Next:nil}
ret := addTwoNumbers(&l1, &l2)
for ret != nil {
fmt.Println(ret.Val)
ret = ret.Next
}
}