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:
// Init a singly linked list [1,2,3]. ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); Solution solution = new Solution(head); // getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning. solution.getRandom();
这道题是在未知长度链表中等概率的随机取节点,题目难度为Medium。
由于每次取随机节点时不知道链表长度,所以不能简单用rand()函数来取随机节点。这里介绍下蓄水池抽样的基本思路,了解的同学可以跳过。假定最终选出的节点为node,第一次直接选第一个节点;第二次以1/2的概率决定是否拿第二个节点替换node;第三次以1/3的概率决定是否拿第三个节点替换node;以此类推,直至链表最后一个节点。这种方法能够保证所有节点被选中的概率均为1/n(n为当前节点数),即保证了等概率的随机选取。证明如下:
对于第m个节点,被选中的概率为:它被选中的概率 * 之后节点都没被选中的概率,即(1/m) * (m/(m+1)) * ((m+1)/(m+2)) * ... * ((n-1)/n) = 1/n.
具体代码:
class Solution {
ListNode* hd;
public:
/** @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at least one node. */
Solution(ListNode* head) : hd(head) {}
/** Returns a random node's value. */
int getRandom() {
ListNode* node = hd;
int cnt = 0, randVal = 0;
while(node) {
if(rand() % (++cnt) == 0)
randVal = node->val;
node = node->next;
}
return randVal;
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/
还可以将问题扩展到在未知或大样本中等概率随机取k个节点,思路是一样的。首先选出前k个节点放入蓄水池;第k+1个节点以k/(k+1)的概率决定是否将其换入蓄水池,如果换入则随机选择k个节点中的一个进行替换;同理第k+2个节点以k/(k+2)的概率决定是否将其换入蓄水池;直至最后节点,最终能够保证所有节点被选中的概率均为k/n,证明如下:
对于第m个节点,被选中的概率为:它被选中的概率 * (之后节点没被选中的概率 + 之后节点被选中但没有替换掉它的概率),即(k/m) * ((m+1-k)/(m+1) + (k/(m+1)) * ((k-1)/k)) * ... * ((n-k)/n + (k/n) * ((k-1)/k)) = k/n.
wiki上蓄水池抽样的页面,感兴趣的同学可以看下:https://en.wikipedia.org/wiki/Reservoir_sampling