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.

**Examples:**

Input:Linked list = 2 -> 3 -> 3 -> 3 -> 4 -> 2, Key = 5

Output:0

Explanation:

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

Output:0.2

**Approach:**

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:

## C

`// C code to find the probability ` `// of finding an Element ` `// in a Singly Linked List ` ` ` `#include <stdio.h> ` `#include <stdlib.h> ` ` ` `// Link list node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* Given a reference (pointer to pointer) ` ` ` `to the head of a list and an int, ` ` ` `push a new node on the front of the list. */` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `// allocate node ` ` ` `struct` `Node* new_node ` ` ` `= (` `struct` `Node*)` `malloc` `( ` ` ` `sizeof` `(` `struct` `Node)); ` ` ` ` ` `// put in the data ` ` ` `new_node->data = new_data; ` ` ` ` ` `// link the old list off the new node ` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `// move the head to point to the new node ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// Counts nnumber of nodes in linked list ` `int` `getCount(` `struct` `Node* head) ` `{ ` ` ` ` ` `// Initialize count ` ` ` `int` `count = 0; ` ` ` ` ` `// Initialize current ` ` ` `struct` `Node* current = head; ` ` ` ` ` `while` `(current != NULL) { ` ` ` `count++; ` ` ` `current = current->next; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `float` `kPresentProbability( ` ` ` `struct` `Node* head, ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Initialize count ` ` ` `float` `count = 0; ` ` ` ` ` `// Initialize current ` ` ` `struct` `Node* current = head; ` ` ` ` ` `while` `(current != NULL) { ` ` ` `if` `(current->data == k) ` ` ` `count++; ` ` ` `current = current->next; ` ` ` `} ` ` ` `return` `count / n; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Start with the empty list ` ` ` `struct` `Node* head = NULL; ` ` ` ` ` `// Use push() to construct below list ` ` ` `// 1->2->1->3->1 ` ` ` `push(&head, 2); ` ` ` `push(&head, 3); ` ` ` `push(&head, 5); ` ` ` `push(&head, 1); ` ` ` `push(&head, 9); ` ` ` `push(&head, 8); ` ` ` `push(&head, 0); ` ` ` `push(&head, 7); ` ` ` `push(&head, 6); ` ` ` `push(&head, 5); ` ` ` ` ` `printf` `(` `"%.1f"` `, ` ` ` `kPresentProbability( ` ` ` `head, getCount(head), 5)); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find the probability ` `# of finding an Element ` `# in a Singly Linked List ` ` ` `# Node class ` `class` `Node: ` ` ` ` ` `def` `__init__(` `self` `, data, ` `next` `=` `None` `): ` ` ` ` ` `self` `.data ` `=` `data ` ` ` `self` `.` `next` `=` `None` ` ` `class` `LinkedList: ` ` ` ` ` `def` `__init__(` `self` `): ` ` ` ` ` `self` `.head ` `=` `None` ` ` ` ` `def` `push(` `self` `, data): ` ` ` ` ` `# Allocate the Node & ` ` ` `# put the data ` ` ` `new_node ` `=` `Node(data) ` ` ` ` ` `# Make the next of new Node as head ` ` ` `new_node.` `next` `=` `self` `.head ` ` ` ` ` `# Move the head to point to new Node ` ` ` `self` `.head ` `=` `new_node ` ` ` ` ` `# Counts the number of nodes in linkedlist ` ` ` `def` `getCount(` `self` `): ` ` ` ` ` `# Initialize current ` ` ` `current ` `=` `self` `.head ` ` ` ` ` `# Initialize count ` ` ` `count ` `=` `0` ` ` ` ` `while` `current ` `is` `not` `None` `: ` ` ` `count ` `+` `=` `1` ` ` `current ` `=` `current.` `next` ` ` ` ` `return` `count ` ` ` ` ` `def` `kPresentProbability(` `self` `, n, k): ` ` ` ` ` `# Initialize current ` ` ` `current ` `=` `self` `.head ` ` ` ` ` `# Initialize count ` ` ` `count ` `=` `0.0` ` ` ` ` `while` `current ` `is` `not` `None` `: ` ` ` `if` `current.data ` `=` `=` `k: ` ` ` `count ` `+` `=` `1` ` ` ` ` `current ` `=` `current.` `next` ` ` ` ` `return` `count ` `/` `n ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Start with empty list ` ` ` `llist ` `=` `LinkedList() ` ` ` ` ` `# Use push to construct the linked list ` ` ` `llist.push(` `2` `) ` ` ` `llist.push(` `3` `) ` ` ` `llist.push(` `5` `) ` ` ` `llist.push(` `1` `) ` ` ` `llist.push(` `9` `) ` ` ` `llist.push(` `8` `) ` ` ` `llist.push(` `0` `) ` ` ` `llist.push(` `7` `) ` ` ` `llist.push(` `6` `) ` ` ` `llist.push(` `5` `) ` ` ` ` ` `print` `(llist.kPresentProbability( ` ` ` `llist.getCount(), ` `5` `)) ` ` ` `# This code is contributed by kevalshah5 ` |

*chevron_right*

*filter_none*

**Output:**

0.2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Create new linked list from two given linked list with greater element at each node
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Count of Prime Nodes of a Singly Linked List
- Sum and Product of all Fibonacci Nodes of 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
- Find probability of selecting element from kth column after N iterations
- Finding 'k' such that its modulus with each array element is same
- Find the element in a linked list with frequency at least N/3
- Generate integer from 1 to 7 with equal probability
- Random number generator in arbitrary probability distribution fashion
- Probability of Knight to remain in the chessboard
- Probability of getting at least K heads in N tosses of Coins
- Aptitude | Probability | Question 1
- Aptitude | Probability | Question 2
- Aptitude | Probability | Question 3
- Aptitude | Probability | Question 4
- Aptitude | Probability | Question 5
- Aptitude | Probability | Question 6
- Aptitude | Probability | Question 7

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.