-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_382.java
More file actions
44 lines (37 loc) · 839 Bytes
/
Solution_382.java
File metadata and controls
44 lines (37 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.hilbert25.leetcode;
import java.util.Random;
/**
* @author : hilbert25
* @version 创建时间:2017年4月5日 下午11:54:30 LeetCode com.hilbert25.leetcode
* Solution_382
*/
public class Solution_382 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Random ran = new Random();
System.out.println(ran.nextInt(1));
}
ListNode head;
Random ran;
public Solution_382(ListNode head) {
this.head = head;
ran = new Random();
}
/** Returns a random node's value. */
public int getRandom() {
ListNode node = head;
int current = 0;
for (int i = 1; node != null; i++) {
current = ran.nextInt(i) == 0 ? node.val : current;
node=node.next;
}
return current;
}
}