blob: 6215989f2d1219fd3db7b841f1708a13c14fee2c (
plain)
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
|
// 382. Linked List Random Node
#include <stddef.h>
#include <stdlib.h>
/*
* given singly linked list, return a random node's value from the linked list.
* each node must have the same probability of being chosen. implement
* 'Solution' class: f1: initialises object with head of signly linked list
* 'head' f2: chooses node randomly from list and returns its value. all nodes
* should be equally likely to be chosen
*/
struct ListNode {
int val;
struct ListNode *next;
};
typedef struct {
struct ListNode *list;
size_t size;
} Solution;
Solution *solutionCreate(struct ListNode *head) {
Solution *obj = (Solution *)malloc(sizeof(Solution));
obj->list = head;
while (head->next)
head = head->next;
head->next = obj->list;
return obj;
}
int solutionGetRandom(Solution *obj) {
struct ListNode *node = obj->list;
int n = rand() % 10;
while (n--)
node = node->next;
return node->val;
}
void solutionFree(Solution *obj) {
free(obj->list);
free(obj);
}
int main() {
struct ListNode *head;
Solution *obj = solutionCreate(head);
int param_1 = solutionGetRandom(obj);
}
|