Skip to content

Commit

Permalink
Time: 28 ms (33.38%), Space: 16.7 MB (57.94%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Mar 10, 2023
1 parent bfffe11 commit 620ec5d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 0382-linked-list-random-node/0382-linked-list-random-node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution
{
vector<int> v;
int n;

public:
Solution(ListNode *head)
{
ListNode *ptr = head;
while (ptr)
{
v.push_back(ptr->val);
ptr = ptr->next;
}
n = v.size();
}

int getRandom()
{
static int i = 0;
if (i == 0)
{
srand(time(NULL));
i++;
}
return v[rand() % n];
}
};

0 comments on commit 620ec5d

Please sign in to comment.