/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
ListNode head;
Random random;
/** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
public Solution(ListNode head) {
random = new Random();
this.head = head;
}
/** Returns a random node's value. */
public int getRandom() {
ListNode currentNode = head;
int returnVal = head.val;
for(int i = 1; currentNode != null; i++, currentNode = currentNode.next){
if(random.nextInt(i) == 0){
returnVal = currentNode.val;
}
}
return returnVal;
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/