Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active February 5, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betandr/815e05164e6bc12e981768f462d7f607 to your computer and use it in GitHub Desktop.
Save betandr/815e05164e6bc12e981768f462d7f607 to your computer and use it in GitHub Desktop.
Summing Integer Linked List
package main
import "fmt"
type IntList struct {
Value int
Next *IntList
}
// A nil `IntList` represents the empty list
func (l *IntList) Sum() int {
if l == nil {
return 0
}
return l.Value + l.Next.Sum()
}
func main() {
h := IntList{1, nil}
i := IntList{2, &h}
j := IntList{3, &i}
k := IntList{4, &j}
l := IntList{5, &k}
fmt.Println(l.Sum())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment