-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlink.py
More file actions
41 lines (34 loc) · 991 Bytes
/
link.py
File metadata and controls
41 lines (34 loc) · 991 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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
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.ll = head
self.length = 0
while head:
self.length += 1
head = head.next
def getRandom(self):
"""
Returns a random node's value.
:rtype: int
"""
import random
r = random.randint(1,self.length)
count = 1
h = self.ll
while h and count <= r:
if count == r:
return h.val
count += 1
h = h.next
# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()