Linked List Random Node -- LeetCode

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.

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?

Example:

1 // Init a singly linked list [1,2,3].
2 ListNode head = new ListNode(1);
3 head.next = new ListNode(2);
4 head.next.next = new ListNode(3);
5 Solution solution = new Solution(head);
6
7 // getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
8 solution.getRandom();

思路:从n个数中选k个数,每个数被选中的几率是k/n的方法:

将n个数的前k个数选出来,构成一个备选集合。然后从第k+1个数开始向后遍历直到第n个数。遍历第k+1个数时,我们用k/(k+1)的几率选中它,然后随机替换掉备选集合中的一个数。则此时对于备选集合中的每个数,不被替换掉的几率是 1 - P(第k+1个数被选中并替换掉该数) = 1 - k/(k+1) * 1/k = k/(k+1)。

假设遍历到第i个数时,一个数留在备选集合中的概率是k/i。则对于第i+1个数,我们用k/(i+1)的概率选中它,并随机替换掉备选集合中的一个数,则对于备选集合中的每一个数,仍然留在集合中的概率是P(该数在上一次流了下来)(1-P(第i+1个数被选中并替换掉该数))= k/i * (1 - k/(i+1) * 1/k) = k/(i+1)。

因此,当我们进行到第n个数时,该数进入备选集合的概率是k/n, 备选集合中其他的数留下的概率是k/n。最后备选集合中的数就是我们要随机选出的k个数,每个数被选出的几率是k/n。

这个题里,让k等于1即可。因此备选集合中始终只有一个值,遍历第i个数时,用1/i的概率选中它并与备选值进行替换。最后的备选值就是随机选出的数,选取的概率为1/n。

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* head;
12     /** @param head The linked list‘s head.
13         Note that the head is guaranteed to be not null, so it contains at least one node. */
14     Solution(ListNode* head) {
15         this->head = head;
16     }
17
18     /** Returns a random node‘s value. */
19     int getRandom() {
20         ListNode* res;
21         int count = 1;
22         for (ListNode* i = head; i != NULL; i = i->next, count++) {
23             srand(time(NULL));
24             int roll = rand() % count + 1;
25             if (roll == 1) res = i;
26         }
27         return res->val;
28     }
29 };
30
31 /**
32  * Your Solution object will be instantiated and called as such:
33  * Solution obj = new Solution(head);
34  * int param_1 = obj.getRandom();
35  */
时间: 2024-09-29 13:54:39

Linked List Random Node -- LeetCode的相关文章

382. Linked List Random Node

/* * 382. Linked List Random Node * 2016-8-20 by Mingyang * 直接进行长度计算就好了,再根据随机数,便利到指定的位置找到结果 */ class Solution { int len = 0; ListNode head; /** @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at le

[LeetCode] 382. Linked List Random Node

https://leetcode.com/problems/linked-list-random-node /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { List<ListNode> array = new ArrayList&l

&lt;LeetCode OJ&gt; 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. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this effi

【Leetcode】Linked List Random Node

题目链接:https://leetcode.com/problems/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. Follow up: What if the linked list is extremely l

[leetcode]-Linked List Random Node

题目描述: 给定一个单向的链表,要求以相等的概率返回 要求: 时间复杂度o(n),空间复杂度o(1) 原文描述: 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. Follow up: What if the linked list is extremely large an

382 Linked List Random Node 链表随机节点

给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 [1,2,3].ListNode head = new ListNode(1);head.next = new ListNode(2);head.next.next = new ListNode(3);Solution solution = new Solution(head);// getRand

*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. Follow up:What if the linked list is extremely large and its length is unknown to you? Could you solve this effic

Reservoir Sampling-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. Follow up:What if the linked list is extremely large and its length is unknown to you? Could you solve this effic

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. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this effi