-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.go
More file actions
46 lines (37 loc) · 724 Bytes
/
2.go
File metadata and controls
46 lines (37 loc) · 724 Bytes
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
package main
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
type ListNode struct {
Val int
Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
return addNum(l1, l2, false)
}
func addNum(l1 *ListNode, l2 *ListNode, carry bool) *ListNode {
if l1 == nil && l2 == nil && carry == false {
return nil
}
sum := 0
if carry == true {
sum ++
}
if l1 != nil {
sum += l1.Val
l1 = l1.Next
}
if l2 != nil {
sum += l2.Val
l2 = l2.Next
}
node := ListNode{
Val: sum % 10,
Next: addNum(l1, l2, sum >= 10),
}
return &node
}