/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
// type Solution struct {
// }
// func Constructor(head *ListNode) Solution {
// }
// func (this *Solution) GetRandom() int {
// }
/**
* Your Solution object will be instantiated and called as such:
* obj := Constructor(head);
* param_1 := obj.GetRandom();
*/
type Solution struct {
vals []int
}
func Constructor(head *ListNode) Solution {
var solution Solution
for {
if head == nil {
break
}
solution.vals = append(solution.vals, head.Val)
head = head.Next
}
return solution
}
func (this *Solution) GetRandom() int {
//fmt.Println(this.vals)
//var loc int = -1
l := len(this.vals)
//fmt.Println(l)
//n := rand.Intn(l)
//fmt.Println(n)
//sum := 0
//for idx, num := range this.vals {
// sum += this.vals[num]
// if sum > n {
// loc = idx
// break
// }
//}
//return loc
return this.vals[rand.Intn(l)]
}