Very basic linked list in Go
type Node struct {
data int
next *Node
}
type LinkedList struct {
head *Node
length int
}
Prepend function
func (l *LinkedList) prepend(n *Node) {
second := l.head
l.head = n
l.head.next = second
l.length += 1
}
Traverse function
func (l LinkedList) traverse() {
current := l.head
for current.next != nil {
fmt.Print(current.data, " -> ")
current = current.next
}
fmt.Println(current.data)
}