# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def __init__(self, head: Optional[ListNode]):
self.nums = []
nums = self.nums
while head != None:
nums.append(head.val)
head = head.next
def getRandom(self) -> int:
n = len(self.nums)
R = random.randint(0, n - 1)
return self.nums[R]
# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()
总结:
random.randint(a, b)两边都可以取到