Given A sorted linked list of elements. The task is to find the median in the given Sorted Linked List.
We know that median in a sorted array is the middle element.
Procedure to find median of N sorted numbers:
if N is odd: median is N/2th element else median is N/2th element + (N/2+1)th element
Input : 1->2->3->4->5->NULL Output : 3 Input : 1->2->3->4->5->6->NULL Output : 3.5
- Traverse the linked list and count all elements.
- if count is odd then again traverse the linked list and find n/2th element.
- if count is even then again traverse the linked list and find:
(n/2th element+ (n/2+1)th element)/2
Note: The above solution traverse the linked list two times.
Efficient Approach: an efficient approach is to traverse the list using two pointers to find the number of elements. See method 2 of this post.
We can use the above algorithm for finding the median of the linked list. Using this algorithm we won’t need to count the number of element:
- if the fast_ptr is Not NULL then it means linked list contain odd element we simply print the data of the slow_ptr.
- else if fast_ptr reach to NULL its means linked list contain even element we create backup of the previous node of slow_ptr and print (previous node of slow_ptr+ slow_ptr->data)/2
Below is the implementation of the above approach:
Median is : 3.5
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.
- Finding Median of unsorted Array in linear time using C++ STL
- Given a linked list which is sorted, how will you insert in sorted way
- Insert value in sorted way in a sorted doubly linked list
- Merge K sorted Doubly Linked List in Sorted Order
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Probability of finding an element K in a Singly Linked List
- Median of two sorted arrays of different sizes
- Median of two sorted arrays with different sizes in O(log(min(n, m)))
- Median of two sorted arrays of different sizes | Set 1 (Linear)
- Find median in row wise sorted matrix
- Median of two sorted arrays of same size
- Sorted merge of two sorted doubly circular linked lists
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- 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
- Create new linked list from two given linked list with greater element at each node
- Check if a linked list is Circular Linked List
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.