Linked List Random Node - Problem
Linked List Random Node is a fascinating problem that combines probability theory with data structures. You're given a singly linked list and need to implement a class that can return a random node's value from the list with equal probability for each node.
The challenge is that linked lists don't provide random access like arrays do - you can't just jump to index 3 or 7. You need to traverse from the head each time. Your task is to implement:
This problem tests your understanding of reservoir sampling, a clever technique for selecting random samples from a stream of unknown length. It's particularly useful when you can't store all elements in memory or when the size is unknown.
The challenge is that linked lists don't provide random access like arrays do - you can't just jump to index 3 or 7. You need to traverse from the head each time. Your task is to implement:
Solution(ListNode head) - Initialize with the linked list headint getRandom() - Return a random node's value where each node has equal probability of being chosenThis problem tests your understanding of reservoir sampling, a clever technique for selecting random samples from a stream of unknown length. It's particularly useful when you can't store all elements in memory or when the size is unknown.
Input & Output
example_1.py โ Basic Usage
$
Input:
list = [1, 2, 3]
solution = Solution(list)
solution.getRandom() # calls
โบ
Output:
Possible outputs: 1, 2, or 3 (each with equal probability)
๐ก Note:
With 3 nodes in the list, each node should be selected approximately 1/3 of the time over many calls. The exact return value is random but the distribution should be uniform.
example_2.py โ Single Node
$
Input:
list = [5]
solution = Solution(list)
solution.getRandom()
โบ
Output:
5
๐ก Note:
When there's only one node, that node must always be selected, so the result is deterministic.
example_3.py โ Multiple Calls
$
Input:
list = [1, 2]
solution = Solution(list)
[solution.getRandom() for _ in range(6)]
โบ
Output:
Possible: [1, 2, 1, 1, 2, 2] or any other combination
๐ก Note:
Each call is independent. With 2 nodes, over many calls we expect roughly 50% to return 1 and 50% to return 2, but individual sequences can vary.
Visualization
Tap to expand
Understanding the Visualization
1
Start the Journey
Begin with first node as your choice - it's 100% likely to be selected so far
2
Second Node Decision
Flip a fair coin (50% chance) - replace or keep? Now both nodes have equal 50% probability
3
Third Node Magic
Roll a 3-sided die - replace with 1/3 probability. All three nodes now have exactly 1/3 probability!
4
Pattern Continues
For nth node, replace with 1/n probability. Mathematics ensures perfect fairness!
Key Takeaway
๐ฏ Key Insight: Reservoir sampling proves that you don't need to know the total size to achieve perfect randomness. By using decreasing replacement probability (1/n for the nth element), each element maintains exactly 1/n final probability of selection!
Time & Space Complexity
Time Complexity
O(n)
Single traversal of the linked list, visiting each node exactly once
โ Linear Growth
Space Complexity
O(1)
Only storing the current result and a counter, constant space
โ Linear Space
Constraints
- The number of nodes in the linked list will be in the range [1, 104]
- -104 <= Node.val <= 104
- At most 104 calls will be made to getRandom
- Follow up: What if the linked list is extremely large and its length is unknown to you?
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code