LeetCode 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.
idea:
uniformly random choose a node from linkedlist
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
/** @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) {
}
/** Returns a random node's value. */
public int getRandom() {
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/
get the whole length of the linkedlist, and random picked one index and move head pointer to there.
we need to proprecess to the length.
class Solution {
/** @param head The linked list's head.
Note that the head is guaranteed to be not null, so it contains at least one node. */
ListNode head;
int len;
Random rand;
public Solution(ListNode head) {
this.head = head;
rand = new Random();
ListNode p = head;
len = 1;
while (p.next != null) {
p = p.next;
len++;
}
}
/** Returns a random node's value. */
public int getRandom() {
int index = rand.nextInt(len);
ListNode p = head;
while (index > 0) {
index--;
p = p.next;
}
return p.val;
}
}
相關文章
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- LeetCode | 141 linked list cycleLeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- Leetcode 206. Reverse Linked ListLeetCode
- Leetcode 234. Palindrome Linked ListLeetCode
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- LeetCode 138. Copy List with Random PointerLeetCoderandom
- [LeetCode] 430. Flatten a Multilevel Doubly Linked ListLeetCode
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- LeetCode707:設計連結串列 Design Linked ListLeetCode
- 資料結構與演算法 | Leetcode 206:Reverse Linked List資料結構演算法LeetCode
- 資料結構與演算法 | Leetcode 141:Linked List Cycle資料結構演算法LeetCode
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM
- 328. Odd Even Linked List
- 92. Reverse Linked List II
- 資料結構與演算法 | Leetcode 876. middle-of-the-linked-list資料結構演算法LeetCode
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- [LeetCode Python 3] 876. Middle of the Linked List(連結串列的中間結點)LeetCodePython
- [LeetCode] 1367. Linked List in Binary Tree 二叉樹中的連結串列LeetCode二叉樹
- 114-Flatten Binary Tree to Linked List
- LeetCode Remove Nth Node From End of List(019)解法總結LeetCodeREM
- LeetCode 之 JavaScript 解答第141題 —— 環形連結串列 I(Linked List Cycle I)LeetCodeJavaScript
- Leetcode(Python3) 19. Remove Nth Node From End of ListLeetCodePythonREM
- LeetCode 138:複製帶隨機指標的連結串列 Copy List with Random PointerLeetCode隨機指標random
- [LeetCode] 528. Random Pick with WeightLeetCoderandom
- Leetcode 160. Intersection of Two Linked ListsLeetCode
- 02-線性結構3 Reversing Linked List (25分)
- [LeetCode] 61. Rotate ListLeetCode
- [LeetCode] 148. Sort ListLeetCode
- LeetCode | 148. Sort ListLeetCode
- Leetcode 61. Rotate ListLeetCode
- 資料結構與演算法 | Leetcode 19. Remove Nth Node From End of List資料結構演算法LeetCodeREM
- 【資料結構與演算法】——連結串列(Linked List)資料結構演算法