-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm382.c
57 lines (42 loc) · 1.03 KB
/
m382.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct {
struct ListNode* head;
int size;
} Solution;
Solution* solutionCreate(struct ListNode* head) {
int size = 0;
struct ListNode* curr = head;
while (curr) {
curr = curr->next;
size++;
}
Solution* output = (struct Solution*) malloc(sizeof(Solution));
output->size = size;
output->head = head;
return output;
}
int solutionGetRandom(Solution* obj) {
int indx = rand() % (obj->size);
struct ListNode* curr = obj->head;
for (int i = 0; i < indx; i++) {
curr = curr->next;
}
return curr->val;
}
// I'm presuming that we don't need to free the linkedlist
// Since that was fed initially as an outside obj
void solutionFree(Solution* obj) {
free(obj);
}
/**
* Your Solution struct will be instantiated and called as such:
* Solution* obj = solutionCreate(head);
* int param_1 = solutionGetRandom(obj);
* solutionFree(obj);
*/