find the average of all nodes in a linked list in go

Here's a code snippet that demonstrates how to find the average of all nodes in a linked list in Go:

main.go
type Node struct {
    value int
    next  *Node
}

func getLinkedListAverage(head *Node) float64 {
    var sum float64
    var count float64

    current := head

    for current != nil {
        sum += float64(current.value)
        count++
        current = current.next
    }

    return sum / count
}
306 chars
20 lines

This function takes the head of a linked list as its argument and returns the average of all nodes. It uses a loop to iterate over each node in the list, accumulate their values in a sum variable, and increment a count variable for each node. Finally, the function calculates and returns the average by dividing the sum by the count.

gistlibby LogSnag