Given a Singly Linked List of size N and another key K, we have to find the probability that the key K is present in the Singly Linked List.
Input: Linked list = 2 -> 3 -> 3 -> 3 -> 4 -> 2, Key = 5
Since the value of Key is 5 which is not present in List, the probability of finding the Key in the Linked List is 0.
Input: Linked list = 2 -> 3 -> 5 -> 1 -> 9 -> 8 -> 0 -> 7 -> 6 -> 5, Key = 5
The probability of finding a Key element K in a Singly Linked List is given below:
Probability = Number of Occurrences of Element K / Size of the Linked List
In our approach, we will first count the number of Element K present in the Singly Linked List and then the probability will be calculated by dividing the number of occurrences of K with the size of the Singly Linked List.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Sum of the nodes of a Singly Linked List
- QuickSort on Singly Linked List
- Binary Search on Singly Linked List
- Insertion Sort for Singly Linked List
- Circular Singly Linked List | Insertion
- Implement a stack using singly linked list
- Alternate Odd and Even Nodes in a Singly Linked List
- Convert a Singly Linked List to an array
- Product of the nodes of a Singly Linked List
- Alternating split of a given Singly Linked List | Set 1
- Various operations on Fibonacci nodes in a Singly Linked list
- Sum and Product of all Fibonacci Nodes of a Singly Linked List
- Find the common nodes in two singly linked list
- Delete all Prime Nodes from a Singly Linked List
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Program to delete all even nodes from a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.