leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0382.cpp (360B)


      1 class Solution {
      2     ListNode *head = nullptr;
      3     int size = 0;
      4 
      5   public:
      6     Solution(ListNode *head) : head(head) {
      7         for (ListNode *p = head; p; p = p->next)
      8             size++;
      9     }
     10 
     11     int getRandom() {
     12         int elem = rand() % size;
     13         ListNode *p = head;
     14         while (elem--)
     15             p = p->next;
     16         return p->val;
     17     }
     18 };