-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLinked_List_Random_Node.cpp
43 lines (40 loc) · 1.24 KB
/
Linked_List_Random_Node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// Reservoir sampling - randomly choosing a sample of k items from a list S containing n items,
// where n is either a very large or unknown number. Typically n is large enough that the list doesn't fit into main memory.
class Solution {
private:
ListNode* head;
default_random_engine generator;
public:
/** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
Solution(ListNode* head) {
this->head = head;
generator = default_random_engine();
}
/** Returns a random node's value. */
int getRandom() {
ListNode* iter = head;
int value = iter->val;
for(int k = 1; iter->next; ++k) {
iter = iter->next;
uniform_int_distribution<int> distribution(0, k);
if(distribution(generator) == k) {
value = iter->val;
}
}
return value;
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/