382. Linked List Random Node


https://leetcode.com/problems/linked-list-random-node/


Runtime: 12 ms, faster than 89.29% of Go online submissions for Linked List Random Node.
Memory Usage: 7.5 MB, less than 46.43% of Go online submissions for Linked List Random Node.



```go
import (
	"fmt"
	"math/rand"
)

type Solution struct {
	l []int
}


/** @param head The linked list's head.
  Note that the head is guaranteed to be not null, so it contains at least one node. */
func Constructor(head *ListNode) Solution {
	//6:24
	l := make([]int, 0)
	node := head
	for node != nil {
		l = append(l, node.Val)
		node = node.Next
	}
	return Solution{l: l}
	//6:27
}


/** Returns a random node's value. */
func (this *Solution) GetRandom() int {
	randomIndex := rand.Intn(len(this.l))
	return this.l[randomIndex]
}
```
