Advertisement

Linked List Random Node - LeetCode 382 Solution

Linked List Random Node - Complete Solution Guide

Linked List Random Node is LeetCode problem 382, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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"] [[

Detailed Explanation

The problem requires us to implement a class that can randomly select a node's value from a singly linked list, with each node having an equal probability of being chosen. The `Solution` class has a constructor that takes the head of the linked list and a `getRandom()` method that returns the value of a randomly selected node. The key constraint is that each node must have the same probability of being selected.

Solution Approach

The solution utilizes Reservoir Sampling to achieve equal probability selection without knowing the length of the linked list beforehand. We iterate through the linked list, and at each node, we have a probability of 1/count to replace the current result with the node's value, where 'count' is the number of nodes visited so far. This ensures that each node has an equal probability of being selected after traversing the entire list.

Step-by-Step Algorithm

  1. Step 1: Initialize `count` to 0 and `result` to 0. These variables will track the number of nodes seen so far and the value of the randomly selected node, respectively.
  2. Step 2: Traverse the linked list starting from the head.
  3. Step 3: For each node encountered, increment the `count` by 1.
  4. Step 4: Generate a random integer between 1 and `count` (inclusive).
  5. Step 5: If the generated random integer is equal to 1 (probability of 1/count), update the `result` to the current node's value.
  6. Step 6: Move to the next node in the linked list and repeat steps 3-5 until the end of the list is reached.
  7. Step 7: Return the `result`.

Key Insights

  • Insight 1: Reservoir Sampling is a technique useful for selecting a random element from a stream of data of unknown length. In this case, the linked list can be treated as a stream of data.
  • Insight 2: To ensure each node has an equal probability of being selected, the probability of selecting the i-th node should be 1/i after processing i nodes.
  • Insight 3: The solution must operate in O(1) space and be able to handle very large linked lists where pre-computing the list length is undesirable or impossible.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Linked List, Math, Reservoir Sampling, Randomized.

Companies

Asked at: Nvidia.