题目:
题解:
typedef struct {
struct ListNode * head;
} Solution;
Solution* solutionCreate(struct ListNode* head) {
Solution * obj = (Solution *)malloc(sizeof(Solution));
assert(obj != NULL);
obj->head = head;
return obj;
}
int solutionGetRandom(Solution* obj) {
int i = 1, ans = 0;
for (struct ListNode * node = obj->head; node; node = node->next) {
if (rand() % i == 0) { // 1/i 的概率选中(替换为答案)
ans = node->val;
}
++i;
}
return ans;
}
void solutionFree(Solution* obj) {
free(obj);
}