Simple linked list in Golang

Viewed 20

Cannot understend in Go methods. Why variable 'list' dont changing after calling addNode ?

type ListNode struct {
    Val  int
    Next *ListNode
}

func main() {
    var list *ListNode

    list.addNode(10)
    list.addNode(20)
}

func (c *ListNode) addNode(val int) {
    new := &ListNode{val, c}
    c = new
}

but if rewrite code to this :

list=list.addNode(10)

and

func (c *ListNode) addNode(val int) *ListNode{
    new := &ListNode{val, c}
    c = new
    retun с 
}

then the list expands. Why doesn't the first variant work?

0 Answers
Related