extremely green in the world of GO - infact this is my first ever program in GO. I'm writing an algorithm that reverses a linked list, specifically from this leetcode. The code snippet below doesn't disclose my algorithm, only the main() function I'm using to test my implementation. After debugging, I found that I fail at the here arrow with the error message panic: runtime error: invalid memory address or nil pointer dereference.
type ListNode struct {
Val int
Next *ListNode
}
func main(){
var list *ListNode
head := list
for i := 0; i <= 5; i++ {
var dummy *ListNode
list.Val = i <--------------------- here
list.Next = dummy
dummy = list
}
result := reverseList(head)
for result != nil{
fmt.Printf("%d\t", result.Val)
}
}
I would really appreciate some discussion to this problem!