[LeetCode] 382. Linked List Random Node

https://leetcode.com/problems/linked-list-random-node

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {

    List<ListNode> array = new ArrayList<>();
    /** @param head The linked list‘s head.
        Note that the head is guaranteed to be not null, so it contains at least one node. */
    public Solution(ListNode head) {
        ListNode p = head;
        while (p != null) {
            array.add(p);
            p = p.next;
        }
    }

    /** Returns a random node‘s value. */
    public int getRandom() {
        Random random = new Random();
        int index = random.nextInt(array.size());
        return array.get(index).val;
    }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(head);
 * int param_1 = obj.getRandom();
 */
时间: 2024-10-20 18:41:33

[LeetCode] 382. Linked List Random Node的相关文章

382. Linked List Random Node

/* * 382. Linked List Random Node * 2016-8-20 by Mingyang * 直接进行长度计算就好了,再根据随机数,便利到指定的位置找到结果 */ class Solution { int len = 0; ListNode head; /** @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at le

&lt;LeetCode OJ&gt; 382. Linked List Random Node

Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this effi

【Leetcode】Linked List Random Node

题目链接:https://leetcode.com/problems/linked-list-random-node/ 题目: Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely l

382 Linked List Random Node 链表随机节点

给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 [1,2,3].ListNode head = new ListNode(1);head.next = new ListNode(2);head.next.next = new ListNode(3);Solution solution = new Solution(head);// getRand

382. Linked List Random Node 链接列表随机节点

Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this effi

[leetcode]-Linked List Random Node

题目描述: 给定一个单向的链表,要求以相等的概率返回 要求: 时间复杂度o(n),空间复杂度o(1) 原文描述: Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large an

Linked List Random Node -- LeetCode

Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up:What if the linked list is extremely large and its length is unknown to you? Could you solve this effic

*Linked List Random Node

Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up:What if the linked list is extremely large and its length is unknown to you? Could you solve this effic

Reservoir Sampling-382. Linked List Random Node

Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up:What if the linked list is extremely large and its length is unknown to you? Could you solve this effic