You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclassSolution:
importrandomdef__init__(self, head: ListNode):
""" @param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. """self.list= []
whilehead:
self.list.append(head.val)
head=head.nextdefgetRandom(self) ->int:
""" Returns a random node's value. """returnrandom.choice(self.list)
# Your Solution object will be instantiated and called as such:# obj = Solution(head)# param_1 = obj.getRandom()