# 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.list = head self.len = 0 cur = head while cur: self.len += 1 cur = cur.next def getRandom(self): """ Returns a random node's value. :rtype: int """ import random rand = random.uniform(0,self.len) i = 0 cur = self.list while i < rand: cur = cur.next i += 1 if cur is None: cur = self.list return cur.val # Your Solution object will be instantiated and called as such: # obj = Solution(head) # param_1 = obj.getRandom()
My python solution
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.