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-Linked List Random NodeLeetCoderandom
- LeetCode Delete Node in a Linked ListLeetCodedelete
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- 237. Delete Node in a Linked List--LeetCode RecorddeleteLeetCode
- leetcode Linked List CycleLeetCode
- Leetcode Copy List with Random PointerLeetCoderandom
- Leetcode Reverse Linked List IILeetCode
- Leetcode Linked List Cycle IILeetCode
- Leetcode Palindrome Linked ListLeetCode
- Leetcode-Copy List with Random PointerLeetCoderandom
- Copy List with Random Pointer leetcode javarandomLeetCodeJava
- [leetcode]linked-list-cycle-iiLeetCode
- LeetCode-Palindrome Linked ListLeetCode
- LeetCode-Odd Even Linked ListLeetCode
- Leetcode-Reverse Linked List IILeetCode
- Leetcode-Linked List Cycle IILeetCode
- leetcode刷題--Reverse Linked ListLeetCode
- Reverse Linked List II leetcode javaLeetCodeJava
- LeetCode | 141 linked list cycleLeetCode
- 每天一道LeetCode--237.Delete Node in a Linked ListLeetCodedelete
- LeetCode 138. Copy List with Random PointerLeetCoderandom
- 【Leetcode】138. Copy List with Random PointerLeetCoderandom
- LeetCode138:Copy List with Random PointerLeetCoderandom
- leetcode138: Copy List with Random PointerLeetCoderandom
- [LeetCode] 328. Odd Even Linked ListLeetCode
- 【Leetcode】141. Linked List CycleLeetCode
- LeetCode141:Linked List CycleLeetCode
- 【LeetCode】Flatten Binary Tree to Linked ListLeetCode
- Leetcode Flatten Binary Tree to Linked ListLeetCode
- leetcode刷題--Remove Linked List ElementsLeetCodeREM
- leetcode141: Linked List CycleLeetCode
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- Leetcode 234. Palindrome Linked ListLeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- 【Leetcode】142.Linked List Cycle IILeetCode
- LeetCode142:Linked List Cycle IILeetCode