-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedListRandomNode.py
More file actions
34 lines (28 loc) · 916 Bytes
/
linkedListRandomNode.py
File metadata and controls
34 lines (28 loc) · 916 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/linked-list-random-node/
# Author: Miao Zhang
# Date: 2021-02-06
# 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: 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.nodes = []
while head:
self.nodes.append(head.val)
head = head.next
def getRandom(self) -> int:
"""
Returns a random node's value.
"""
return self.nodes[random.randint(0, len(self.nodes) - 1)]
# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()