Skip to content

Commit 9def962

Browse files
committed
New Problem Solution - "Linked List Random Node"
1 parent 6906cda commit 9def962

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LeetCode
1414
|385|[Mini Parser](https://leetcode.com/problems/mini-parser/) | [C++](./algorithms/cpp/miniParser/MiniParser.cpp)|Medium|
1515
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [C++](./algorithms/cpp/shuffleAnArray/ShuffleAnArray.cpp)|Medium|
1616
|383|[Ransom Note](https://leetcode.com/problems/ransom-note/) | [C++](./algorithms/cpp/ransomNote/RansomNote.cpp)|Easy|
17+
|382|[Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/) | [C++](./algorithms/cpp/linkedListRandomNode/LinkedListRandomNode.cpp)|Medium|
1718
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | [C++](./algorithms/cpp/combinationSumIV/combinationSumIV.cpp)|Medium|
1819
|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | [C++](./algorithms/cpp/wiggleSubsequence/wiggleSubsequence.cpp)|Medium|
1920
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [C++](./algorithms/cpp/intersectionOfTwoArraysII/intersectionOfTwoArraysII.cpp)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Source : https://leetcode.com/problems/linked-list-random-node/
2+
// Author : Hao Chen
3+
// Date : 2016-08-24
4+
5+
/***************************************************************************************
6+
*
7+
* Given a singly linked list, return a random node's value from the linked list. Each
8+
* node must have the same probability of being chosen.
9+
*
10+
* Follow up:
11+
* What if the linked list is extremely large and its length is unknown to you? Could
12+
* you solve this efficiently without using extra space?
13+
*
14+
* Example:
15+
*
16+
* // Init a singly linked list [1,2,3].
17+
* ListNode head = new ListNode(1);
18+
* head.next = new ListNode(2);
19+
* head.next.next = new ListNode(3);
20+
* Solution solution = new Solution(head);
21+
*
22+
* // getRandom() should return either 1, 2, or 3 randomly. Each element should have
23+
* equal probability of returning.
24+
* solution.getRandom();
25+
***************************************************************************************/
26+
27+
/**
28+
* Definition for singly-linked list.
29+
* struct ListNode {
30+
* int val;
31+
* ListNode *next;
32+
* ListNode(int x) : val(x), next(NULL) {}
33+
* };
34+
*/
35+
class Solution {
36+
public:
37+
/** @param head The linked list's head.
38+
Note that the head is guaranteed to be not null, so it contains at least one node. */
39+
Solution(ListNode* head) {
40+
this->head = head;
41+
this->len = 0;
42+
for(ListNode*p = head; p!=NULL; p=p->next, len++);
43+
srand(time(NULL));
44+
}
45+
46+
/** Returns a random node's value. */
47+
int getRandom() {
48+
int pos = rand() % len;
49+
ListNode *p = head;
50+
for (; pos > 0; pos--, p=p->next);
51+
return p->val;
52+
}
53+
ListNode* head;
54+
int len;
55+
};
56+
57+
/**
58+
* Your Solution object will be instantiated and called as such:
59+
* Solution obj = new Solution(head);
60+
* int param_1 = obj.getRandom();
61+
*/

0 commit comments

Comments
 (0)