I have a single direction linked list without knowing its size.
I want to get a random element in this list, and I just have one time chance to traverse the list. (I am not allowed to traverse twice or more)
What’s the algorithm for this problem? Thanks!
This is just reservoir sampling with a reservoir of size 1.
Essentially it is really simple
This is uniformly sampled, since the probability of picking any element at the end of the day is 1/n (exercise to the reader).