...
代码块 |
---|
int getRandom(ListNode *head) { int ans; int i = 01; for(auto node = head; node != nullptr; node = node->next) { if(rand() % i == 0) { // 第i个结点选中的概率为1/i,如果命中 ans = node->val; } i++; } return ans; } |
...
...
代码块 |
---|
int getRandom(ListNode *head) { int ans; int i = 01; for(auto node = head; node != nullptr; node = node->next) { if(rand() % i == 0) { // 第i个结点选中的概率为1/i,如果命中 ans = node->val; } i++; } return ans; } |
...