[LeetCode] 382. Linked List Random Node 链表随机结点

 

Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.

Implement the Solution class:

  • Solution(ListNode head) Initializes the object with the integer array nums.
  • int getRandom() Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be choosen.

 

Example 1:

Input
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
Output
[null, 1, 3, 2, 2, 3]

Explanation
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.

 

Constraints:

  • The number of nodes in the linked list will be in the range [1, 104].
  • -104 <= Node.val <= 104
  • At most 104 calls will be made to getRandom.

 

Follow up:

  • What if the linked list is extremely large and its length is unknown to you?
  • Could you solve this efficiently without using extra space?

 

这道题给了一个链表,让随机返回一个结点,那么最直接的方法就是先统计出链表的长度,然后根据长度随机生成一个位置,然后从开头遍历到这个位置即可,参见代码如下:

 

解法一:

class Solution {
public:
    Solution(ListNode* head) {
        len = 0;
        ListNode *cur = head;
        this->head = head;
        while (cur) {
            ++len;
            cur = cur->next;
        }
    }
    int getRandom() {
        int t = rand() % len;
        ListNode *cur = head;
        while (t) {
            --t;
            cur = cur->next;
        }
        return cur->val;
    }
private:
    int len;
    ListNode *head;
};

 

Follow up 中说链表可能很长,没法提前知道长度,这里用到了著名了 水塘抽样 Reservoir Sampling 的思路,由于限定了 head 一定存在,所以先让返回值 res 等于 head 的结点值,然后让 cur 指向 head 的下一个结点,定义一个变量i,初始化为2,若 cur 不为空则开始循环,在 [0, i - 1] 中取一个随机数,如果取出来0,则更新 res 为当前的 cur 的节点值,然后此时i自增一,cur 指向其下一个位置,这里其实相当于维护了一个大小为1的水塘,然后随机数生成为0的话,交换水塘中的值和当前遍历到的值,这样可以保证每个数字的概率相等,参见代码如下:

 

解法二:

class Solution {
public:
    Solution(ListNode* head) {
        this->head = head;
    }
    int getRandom() {
        int res = head->val, i = 2;
        ListNode *cur = head->next;
        while (cur) {
            int j = rand() % i;
            if (j == 0) res = cur->val;
            ++i;
            cur = cur->next;
        }
        return res;
    }
private:
    ListNode *head;
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/382

 

类似题目:

Random Pick Index

 

参考资料:

https://leetcode.com/problems/linked-list-random-node/

https://leetcode.com/problems/linked-list-random-node/discuss/85662/Java-Solution-with-cases-explain

https://leetcode.com/problems/linked-list-random-node/discuss/85659/Brief-explanation-for-Reservoir-Sampling

https://leetcode.com/problems/linked-list-random-node/discuss/85701/O(n)-Time-and-O(1)-Space-Java-Solution

https://leetcode.com/problems/linked-list-random-node/discuss/85690/using-reservoir-sampling-o1-space-on-time-complexityuff0cc

 

LeetCode All in One 题目讲解汇总(持续更新中...)

posted @ 2016-08-11 10:17  Grandyang  阅读(15443)  评论(7编辑  收藏  举报
Fork me on GitHub