-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_val_ll.py
More file actions
42 lines (35 loc) · 1.01 KB
/
Copy pathrandom_val_ll.py
File metadata and controls
42 lines (35 loc) · 1.01 KB
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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
import random
class Solution(object):
def __init__(self, head):
"""
@param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node.
:type head: ListNode
"""
self.head = head
current = head
self.num = 0
while current is not None:
self.num += 1
current = current.next
def getRandom(self):
"""
Returns a random node's value.
:rtype: int
"""
current = None
r = random.randint(0, self.num - 1)
for i in range(r + 1):
if i == 0:
current = self.head
else:
current = current.next
return current.val
# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()