-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path382.cpp
More file actions
32 lines (29 loc) · 734 Bytes
/
Copy path382.cpp
File metadata and controls
32 lines (29 loc) · 734 Bytes
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
// WTF.cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
vector<ListNode *> arr;
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) {
while (head != nullptr) {
arr.push_back(head);
head = head->next;
}
}
/** Returns a random node's value. */
int getRandom() { return arr[(rand() % arr.size())]->val; }
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/