```
public class Solution {
/** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
ListNode head;
public Solution(ListNode head) {
this.head = head;
}
/** Returns a random node's value. */
public int getRandom() {
int len = 0;
ListNode curr=head;
while(curr!=null){
len++;
curr=curr.next;
}
curr = head;
int min = 1;
int max = len;
int random = (int)(min+((max-min)+1)*Math.random());
if(random==1){
return curr.val;
}
for(int i=1; i<=random-1; i++){
curr=curr.next;
}
return curr.val;
}
}
```