## Tuesday, August 9, 2016

### LeetCode 382 - Linked List Random Node

https://discuss.leetcode.com/topic/53738/o-n-time-o-1-space-java-solution
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.
What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?
Example:

```// Init a singly linked list [1,2,3].

// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
solution.getRandom();
```

https://discuss.leetcode.com/topic/53740/reservoir-sampling-java-solution
``````    ListNode head;
Random random;
/** @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at least one node. */
random = new Random();
}

/** Returns a random node's value. */
public int getRandom() {
int size = 1;
while (cur != null) {
if (random.nextInt(size) == 0) {
result = cur;
}
size++;
cur = cur.next;
}

return result.val;
}``````
https://discuss.leetcode.com/topic/53738/o-n-time-o-1-space-java-solution
``````    ListNode head = null;
Random randomGenerator = null;
this.randomGenerator = new Random();

}

/** Returns a random node's value. */
public int getRandom() {
ListNode result = null;

for(int n = 1; current!=null; n++) {
if (randomGenerator.nextInt(n) == 0) {
result = current;
}
current = current.next;
}

return result.val;

}``````

https://discuss.leetcode.com/topic/53739/straight-forward-java-solution