-
Notifications
You must be signed in to change notification settings - Fork 1
/
llist-add-twonos-optimal.go
96 lines (77 loc) · 1.5 KB
/
llist-add-twonos-optimal.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
package main
import (
"fmt"
"math"
)
type Node struct {
val int
next *Node
prev *Node
}
type List struct {
head *Node
tail *Node
}
func (l *List)insertback_list(v int) {
n := &Node {
val: v,
prev: l.tail,
}
if l.tail != nil {
l.tail.next = n
}
l.tail = n
if l.head == nil {
l.head = n
}
}
func (l *List)show_list() {
n := l.head
fmt.Println("print list elements")
for n != nil {
fmt.Printf("%d\n", n.val)
n = n.next
}
}
func add_lists_return_reverselist(l1 *List, l2 *List) (List){
//push list elements to a number
sum1 := 0
sum2 := 0
counter1 := 0
counter2 := 0
n1 := l1.head
for n1 != nil {
sum1 = (int(math.Pow(10,float64(counter1))) * n1.val) + sum1
n1 = n1.next
counter1 = counter1 + 1
}
n2 := l2.head
for n2 != nil {
sum2 = (int(math.Pow(10,float64(counter2))) * n2.val) + sum2
n2 = n2.next
counter2 = counter2 + 1
}
fmt.Println(sum1)
fmt.Println(sum2)
sum := sum1 + sum2
//move the final sum to a list
l := List{}
for sum > 0 {
endlist_elem := sum%10
sum = sum/10
l.insertback_list(endlist_elem)
}
return l
}
func main() {
l1 := List{}
l2 := List{}
l1.insertback_list(9)
l1.insertback_list(9)
l1.insertback_list(9)
l2.insertback_list(9)
l2.insertback_list(9)
l2.insertback_list(9)
l := add_lists_return_reverselist(&l1, &l2)
l.show_list()
}