Last active
February 5, 2019 17:04
-
-
Save betandr/815e05164e6bc12e981768f462d7f607 to your computer and use it in GitHub Desktop.
Summing Integer Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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