leetcode: Reservoir Sampling:Linked List Random Node(382)

原创 2016年08月31日 14:45:38

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:

// Init a singly linked list [1,2,3].
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
Solution solution = new Solution(head);

// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
solution.getRandom();

class Solution {  
    ListNode* hd;  
public:  
    /** @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at least one node. */  
    Solution(ListNode* head) : hd(head) {}  

    /** Returns a random node's value. */  
    int getRandom() {  
        ListNode* node = hd;  
        int cnt = 0, randVal = 0;  
        while(node) {  
            if(rand() % (++cnt) == 0)  
                randVal = node->val;  
            node = node->next;  
        }  
        return randVal;  
    }  
};  

/** 
 * Your Solution object will be instantiated and called as such: 
 * Solution obj = new Solution(head); 
 * int param_1 = obj.getRandom(); 
 */  
版权声明:本文为博主原创文章,未经博主允许不得转载。

[leetcode] 382. Linked List Random Node 解题报告

题目链接: https://leetcode.com/problems/linked-list-random-node/ Given a singly linked list, return a ...
  • qq508618087
  • qq508618087
  • 2016年08月12日 04:45
  • 3462

[编程题] LeetCode上的Reservoir Sampling(蓄水池算法)类型的题目

目前LeetCode上Reservoir Sampling只有两道:Linked List Random Node给定一个单链表,随机返回链表中一个节点的值,每个结点被选中的概率相等。注意: 这个链...
  • notHeadache
  • notHeadache
  • 2016年09月18日 22:25
  • 1359

382. Linked List Random Node (蓄水池抽样,java)

382. Linked List Random Node DescriptionSubmissionsSolutions Total Accepted: 23716Total...
  • CriminalCode
  • CriminalCode
  • 2017年04月10日 15:27
  • 265

【Leetcode】Linked List Random Node

题目链接:https://leetcode.com/problems/linked-list-random-node/ 题目: Given a singly linked list, return...
  • yeqiuzs
  • yeqiuzs
  • 2016年08月10日 11:09
  • 1954

[382]Linked List Random Node

【题目描述】 Given a singly linked list, return a random node's value from the linked list. Each node...
  • sadjuno
  • sadjuno
  • 2016年09月03日 13:44
  • 71

[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 t...
  • u013805817
  • u013805817
  • 2017年01月03日 21:37
  • 212

【LeetCode】Copy List with Random Pointer 解题报告

A linked list is given such that each node contains an additional random pointer which could point t...
  • ljiabin
  • ljiabin
  • 2014年09月04日 17:01
  • 3351

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 t...
  • xjpk0
  • xjpk0
  • 2017年07月01日 11:52
  • 75

LeetCode #382: Linked List Random Node

Problem Statement(Problem Link) Given a singly linked list, return a random node’s value from the li...
  • junchen1992
  • junchen1992
  • 2016年08月29日 09:06
  • 306

leetcode 382 Linked List Random Node C++

这个题因为不知道n是多大,不然直接对n取余即可。 所以这个题要用到蓄水池算法。 蓄水池算法编程珠玑上面有。 在不知道有多少个数的情况下,取出k个数,且每个数被取到的概率都相同。 第m个对象被选...
  • a2331046
  • a2331046
  • 2016年08月31日 20:44
  • 271
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode: Reservoir Sampling:Linked List Random Node(382)
举报原因:
原因补充:

(最多只允许输入30个字)