1234567891011121314151617181920212223242526272829303132 |
- /**
- * Definition for singly-linked list.
- * type ListNode struct {
- * Val int
- * Next *ListNode
- * }
- */
- type Solution struct {
- arr []int
- n 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) (sol Solution) {
- for curr := head; curr != nil; curr = curr.Next {
- sol.arr = append(sol.arr, curr.Val)
- sol.n++
- }
- return
- }
- /** Returns a random node's value. */
- func (this *Solution) GetRandom() int {
- return this.arr[rand.Intn(this.n)]
- }
- /**
- * Your Solution object will be instantiated and called as such:
- * obj := Constructor(head);
- * param_1 := obj.GetRandom();
- */
|