GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/LinkedListRandomNode.java at master · Tulpen/Leetcode-Java Expand file tree/ Copy pathLinkedListRandomNode.javaMore file actions 29 lines (22 loc) · 677 Bytes / Copy pathLinkedListRandomNode.javaFile metadata and controls29 lines (22 loc) · 677 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 import java.util.Random; public class LinkedListRandomNode { private Random mRandom; private ListNode mHead; /** @param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. */ public LinkedListRandomNode(ListNode head) { mRandom = new Random(); mHead = head; } /** Returns a random node's value. */ public int getRandom() { int count = 0; int value = -1; for (ListNode p = mHead; p != null; p = p.next) { if (mRandom.nextInt(++count) == 0) { value = p.val; } } return value; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |