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);
 */
相关推荐
呆呆的猫14 分钟前
【LeetCode】227、基本计算器 II
算法·leetcode·职场和发展
Tisfy16 分钟前
LeetCode 1705.吃苹果的最大数目:贪心(优先队列) - 清晰题解
算法·leetcode·优先队列·贪心·
余额不足1213837 分钟前
C语言基础十六:枚举、c语言中文件的读写操作
linux·c语言·算法
罗伯特祥2 小时前
C调用gnuplot绘图的方法
c语言·plot
嵌入式科普3 小时前
嵌入式科普(24)从SPI和CAN通信重新理解“全双工”
c语言·stm32·can·spi·全双工·ra6m5
火星机器人life3 小时前
基于ceres优化的3d激光雷达开源算法
算法·3d
虽千万人 吾往矣3 小时前
golang LeetCode 热题 100(动态规划)-更新中
算法·leetcode·动态规划
arnold664 小时前
华为OD E卷(100分)34-转盘寿司
算法·华为od
ZZTC4 小时前
Floyd算法及其扩展应用
算法
lqqjuly4 小时前
特殊的“Undefined Reference xxx“编译错误
c语言·c++