-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00002-add_two_numbers.go
67 lines (54 loc) · 1.06 KB
/
00002-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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 2: Add Two Numbers
// https://leetcode.com/problems/add-two-numbers/
package main
import "fmt"
type ListNode struct {
val int
next *ListNode
}
type LinkedList struct {
head *ListNode
}
func llInput(head **ListNode, inputs []int) {
for i:=len(inputs)-1; i>=0; i-- {
node := &ListNode{inputs[i], *head}
*head = node
}
}
func llOutput(head *ListNode) {
for head != nil {
fmt.Print(head.val, " -> ")
head = head.next
}
fmt.Println("NULL")
}
// SOLUTION
func addTwoNumbers(l1, l2 *ListNode) *ListNode {
head := &ListNode{}
t := head
c := 0
for c!=0 || l1!=nil || l2!=nil {
if l1!=nil {
c += l1.val
l1 = l1.next
}
if l2!=nil {
c += l2.val
l2 = l2.next
}
t.next = &ListNode{val: c%10}
t = t.next
c /= 10
}
return head.next
}
func main() {
ll1, ll2 := LinkedList{}, LinkedList{}
// INPUT
li1, li2 := []int{2,4,3}, []int{5,6,4}
llInput(&ll1.head, li1)
llInput(&ll2.head, li2)
// OUTPUT
result := addTwoNumbers(ll1.head, ll2.head)
llOutput(result)
}