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);
 */
相关推荐
米粒13 小时前
力扣算法刷题 Day 27
算法·leetcode·职场和发展
Fuxiao___4 小时前
C 语言核心知识点讲义(循环 + 函数篇)
算法·c#
Mr_Xuhhh5 小时前
LeetCode hot 100(C++版本)(上)
c++·leetcode·哈希算法
漫随流水5 小时前
c++编程:反转字符串(leetcode344)
数据结构·c++·算法
笨笨饿5 小时前
20_Git 仓库使用手册 - 初学者指南
c语言·开发语言·嵌入式硬件·mcu·学习
C++ 老炮儿的技术栈6 小时前
分享一个安全的CString
c语言·c++·windows·git·安全·visual studio
穿条秋裤到处跑6 小时前
每日一道leetcode(2026.03.31):字典序最小的生成字符串
算法·leetcode
爱编码的小八嘎7 小时前
C语言完美演绎6-2
c语言
CoovallyAIHub8 小时前
VisionClaw:智能眼镜 + Gemini + Agent,看一眼就能帮你搜、帮你发、帮你做
算法·架构·github
CoovallyAIHub8 小时前
低空安全刚需!西工大UAV-DETR反无人机小目标检测,参数减少40%,mAP50:95提升6.6个百分点
算法·架构·github