-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
110 lines (105 loc) · 1.73 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
1.避免生成不必要的节点
2.在同一个func中尽量避免相同条件多次调用
*/
package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
head := &ListNode{}
now := head
carry := 0
for {
x := 0
if l1 != nil {
x = l1.Val
l1 = l1.Next
}
y := 0
if l2 != nil {
y = l2.Val
l2 = l2.Next
}
sum := x + y + carry
carry = sum / 10
now.Val = sum % 10
if l1 == nil && l2 == nil {
break
}
now.Next = &ListNode{}
now = now.Next
}
if carry > 0 {
now.Next = &ListNode{Val: carry}
}
return head
}
func main() {
c := &ListNode{3, nil}
b := &ListNode{2, c}
a := &ListNode{1, b}
y := &ListNode{8, nil}
x := &ListNode{7, y}
//321+87=1308
res := addTwoNumbers(a, x)
for res != nil {
fmt.Print(res.Val)
res = res.Next
}
}
// wrong answer by Leo
//func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
// out := new(ListNode)
// out.Val = 0
// cur := out
// carry := 0
// if l1 == nil {
// out = l2
// return out
// }
// if l2 == nil {
// out = l1
// return out
// }
// if l1 == nil && l2 == nil {
// return out
// }
// for l1.Next != nil || l2.Next != nil {
// fmt.Println(l1, l2)
// x, y := 0, 0
// if l1 != nil {
// x = l1.Val
// } else {
// x = 0
// }
// if l2 != nil {
// y = l2.Val
// } else {
// y = 0
// }
// sum := x + y + carry
// carry = sum / 10
// cur.Next = &ListNode{Val: sum % 10}
// cur = cur.Next
// if l1 != nil {
// l1 = l1.Next
// }
// if l2 != nil {
// l2 = l2.Next
// }
// }
// if (carry) > 0 {
// cur.Next = &ListNode{Val: carry}
// }
// return out.Next
//}