/** @param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. */ private ListNode head; publicSolution(ListNode head){ this.head = head; } /** Returns a random node's value. */ publicintgetRandom(){ int count = 0; ListNode now = head; int ret = -1; Random random = new Random();
while (now != null){ count += 1;
if (ret == -1){ ret = now.val; now = now.next; continue; } int rm = random.nextInt(count );
一、预备知识Java 随机数生成的方法:java.util.Random123456789101112131415161718192021/* Main.java */public class Main { public static void main(String[] args) { // 指定随机种子,...