LeetCode //C - 382. Linked List Random Node

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 head of the singly-linked list head.
  • 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 chosen.
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 , 1 0 4 ] [1, 10^4] [1,104].
  • − 1 0 4 < = N o d e . v a l < = 1 0 4 -10^4 <= Node.val <= 10^4 −104<=Node.val<=104
  • At most 1 0 4 10^4 104 calls will be made to getRandom.

From: LeetCode

Link: 382. Linked List Random Node


Solution:

Ideas:
  1. solutionCreate: Initializes the Solution object, stores the head of the linked list, and seeds the random number generator.

  2. solutionGetRandom: Implements reservoir sampling to randomly select a node with equal probability. It iterates over the entire linked list and replaces the current result with a random chance, ensuring each node has an equal chance of being picked.

  3. solutionFree: Frees the memory allocated for the Solution object.

Code:
c 复制代码
// Definition for singly-linked list.
struct {
    int val;
    struct ListNode *next;
}ListNode;

typedef struct {
    struct ListNode* head;
} Solution;

Solution* solutionCreate(struct ListNode* head) {
    Solution* obj = (Solution*) malloc(sizeof(Solution));
    obj->head = head;
    srand(time(NULL)); // Initialize random number generator
    return obj;
}

int solutionGetRandom(Solution* obj) {
    struct ListNode* current = obj->head;
    int result = current->val;
    int i = 1;
    
    while (current != NULL) {
        // Pick a random number in range [0, i]
        if (rand() % i == 0) {
            result = current->val; // Replace the result with current node's value
        }
        current = current->next;
        i++;
    }
    return result;
}

void solutionFree(Solution* obj) {
    free(obj);
}

/**
 * Your Solution struct will be instantiated and called as such:
 * Solution* obj = solutionCreate(head);
 * int param_1 = solutionGetRandom(obj);
 * solutionFree(obj);
 */
相关推荐
纪元A梦2 分钟前
贪心算法应用:保险理赔调度问题详解
算法·贪心算法
Florence236 分钟前
计算机组成原理:GPU架构、并行计算、内存层次结构等
c语言
Jayden_Ruan1 小时前
C++逆向输出一个字符串(三)
开发语言·c++·算法
不吃鱼的羊1 小时前
启动文件Startup_vle.c
c语言·开发语言
点云SLAM2 小时前
C++ 常见面试题汇总
java·开发语言·c++·算法·面试·内存管理
叙白冲冲2 小时前
哈希算法以及面试答法
算法·面试·哈希算法
YuTaoShao3 小时前
【LeetCode 每日一题】1277. 统计全为 1 的正方形子矩阵
算法·leetcode·矩阵
古译汉书3 小时前
嵌入式铁头山羊stm32-ADC实现定时器触发的注入序列的单通道转换-Day26
开发语言·数据结构·stm32·单片机·嵌入式硬件·算法
野犬寒鸦3 小时前
力扣hot100:相交链表与反转链表详细思路讲解(160,206)
java·数据结构·后端·算法·leetcode
阿昭L3 小时前
leetcode两数之和
算法·leetcode