LeetCode – Linked List Random Node (Java)

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.

Follow up:
What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?

Java Solution

This problem is trivial, so I focus on the follow-up problem.

We want the probability of being chosen is 1/count.

Given a list 1 -> 2 -> 3 -> 4 -> 5 -> … -> n, the only time we can possibly select the third node is when the pointer points to 3. The probability of selecting the third node is 1/3 * 3/4 * 4/5 * … * (n-1)/n = 1/n.

public class Solution {
 
    Random r = null;
    ListNode h = null;
 
    public Solution(ListNode head) {
        r = new Random();
        h = head;
    }
 
    /** Returns a random node's value. */
    public int getRandom() {
        int count=1;
        ListNode p = h;
        int result = 0;
        while(p!=null){
            if(r.nextInt(count)==0)
                result= p.val;
            count++;
            p = p.next;
        }
        return result;
    }
}

1 thought on “LeetCode – Linked List Random Node (Java)”

  1. Why wouldn’t having the original head rather than a temp p not work? I ran into the same problem with my project 😮

Leave a Comment