Skip to content

Commit ce99094

Browse files
committed
Time: 20 ms (85.48%), Space: 16.8 MB (8.14%) - LeetHub
1 parent 5623ac7 commit ce99094

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
vector<int> v;
14+
Solution(ListNode* head) {
15+
while(head)
16+
{
17+
v.push_back(head->val);
18+
head = head->next;
19+
}
20+
}
21+
22+
int getRandom() {
23+
return v[rand()%v.size()];
24+
}
25+
};
26+
27+
/**
28+
* Your Solution object will be instantiated and called as such:
29+
* Solution* obj = new Solution(head);
30+
* int param_1 = obj->getRandom();
31+
*/

0 commit comments

Comments
 (0)