Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.
Follow up:
What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?
Example:
解法1: Reservoir Sampling
是reservoir sampling的直接应用。从discussion上看来的比较好懂的解释:
|
|