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);
 */
相关推荐
需要8269 分钟前
分布式 ID 生成:雪花算法、号段模式与时钟回拨
分布式·算法
垆边人似月.16 分钟前
华为机考题:字符串分隔
算法·华为
CQU_JIAKE19 分钟前
9.4【A】
算法
CQU_JIAKE23 分钟前
9.13[A]
算法
懿路向前33 分钟前
【唤醒实战笔记】2026-09-29 | 智能体提示词迭代史(v1 → v8)
linux·笔记·算法
@陈小鱼1 小时前
基于CNN-Transformer的无袖带血压估计
人工智能·深度学习·神经网络·算法·cnn·transformer·血压
大侠归来1 小时前
C语言内存管理:从栈到堆的完整指南
c语言·开发语言·python
cu1431 小时前
细谈GM7123C的具体功能与其应用
c语言·c++·人工智能·嵌入式硬件
黄金龙PLUS1 小时前
5个1280比特大状态置换算法的设计与分析
算法·网络安全·密码学·哈希算法·同态加密
不会就选b1 小时前
算法日常・每日刷题--<动态规划>1
java·数据结构·算法