|
| 1 | +# [382. Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/) |
| 2 | + |
| 3 | + |
| 4 | +## 题目 |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +Implement the `Solution` class: |
| 9 | + |
| 10 | +- `Solution(ListNode head)` Initializes the object with the integer array nums. |
| 11 | +- `int getRandom()` Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be choosen. |
| 12 | + |
| 13 | +**Example 1:** |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +``` |
| 18 | +Input |
| 19 | +["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"] |
| 20 | +[[[1, 2, 3]], [], [], [], [], []] |
| 21 | +Output |
| 22 | +[null, 1, 3, 2, 2, 3] |
| 23 | +
|
| 24 | +Explanation |
| 25 | +Solution solution = new Solution([1, 2, 3]); |
| 26 | +solution.getRandom(); // return 1 |
| 27 | +solution.getRandom(); // return 3 |
| 28 | +solution.getRandom(); // return 2 |
| 29 | +solution.getRandom(); // return 2 |
| 30 | +solution.getRandom(); // return 3 |
| 31 | +// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning. |
| 32 | +
|
| 33 | +``` |
| 34 | + |
| 35 | +**Constraints:** |
| 36 | + |
| 37 | +- The number of nodes in the linked list will be in the range `[1, 104]`. |
| 38 | +- `-10^4 <= Node.val <= 10^4` |
| 39 | +- At most `10^4` calls will be made to `getRandom`. |
| 40 | + |
| 41 | +**Follow up:** |
| 42 | + |
| 43 | +- What if the linked list is extremely large and its length is unknown to you? |
| 44 | +- Could you solve this efficiently without using extra space? |
| 45 | + |
| 46 | +## 题目大意 |
| 47 | + |
| 48 | +给定一个单链表,随机选择链表的一个节点,并返回相应的节点值。保证每个节点被选的概率一样。 |
| 49 | + |
| 50 | +进阶: 如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现? |
| 51 | + |
| 52 | +## 解题思路 |
| 53 | + |
| 54 | +- rand.Float64() 可以返回 [0.0,1.0) 之间的随机数。利用这个函数完成我们的随机化取节点的过程。 |
| 55 | + |
| 56 | +## 代码 |
| 57 | + |
| 58 | +```go |
| 59 | +package leetcode |
| 60 | + |
| 61 | +import ( |
| 62 | + "math/rand" |
| 63 | + |
| 64 | + "github.com/halfrost/LeetCode-Go/structures" |
| 65 | +) |
| 66 | + |
| 67 | +// ListNode define |
| 68 | +type ListNode = structures.ListNode |
| 69 | + |
| 70 | +/** |
| 71 | + * Definition for singly-linked list. |
| 72 | + * type ListNode struct { |
| 73 | + * Val int |
| 74 | + * Next *ListNode |
| 75 | + * } |
| 76 | + */ |
| 77 | +type Solution struct { |
| 78 | + head *ListNode |
| 79 | +} |
| 80 | + |
| 81 | +/** @param head The linked list's head. |
| 82 | + Note that the head is guaranteed to be not null, so it contains at least one node. */ |
| 83 | +func Constructor(head *ListNode) Solution { |
| 84 | + return Solution{head: head} |
| 85 | +} |
| 86 | + |
| 87 | +/** Returns a random node's value. */ |
| 88 | +func (this *Solution) GetRandom() int { |
| 89 | + scope, selectPoint, curr := 1, 0, this.head |
| 90 | + for curr != nil { |
| 91 | + if rand.Float64() < 1.0/float64(scope) { |
| 92 | + selectPoint = curr.Val |
| 93 | + } |
| 94 | + scope += 1 |
| 95 | + curr = curr.Next |
| 96 | + } |
| 97 | + return selectPoint |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Your Solution object will be instantiated and called as such: |
| 102 | + * obj := Constructor(head); |
| 103 | + * param_1 := obj.GetRandom(); |
| 104 | + */ |
| 105 | +``` |
0 commit comments