题目链接:https://leetcode-cn.com/problems/linked-list-random-node/
题目如下:
class Solution {
public:
ListNode* p;
Solution(ListNode* head) {
p=head;
}
int getRandom() {
int res=0;
int cnt=0;
auto temp=p;
while(temp){
cnt++;
if(rand()%cnt==0) res=temp->val;
temp=temp->next;
}
return res;
}
};



