Given a singly linked list and a key, find key using binary search approach.
To perform a Binary search based on Divide and Conquer Algorithm, determination of the middle element is important. Binary Search is usually fast and efficient for arrays because accessing the middle index between two given indices is easy and fast(Time Complexity O(1)). But memory allocation for the singly linked list is dynamic and non-contiguous, which makes finding the middle element difficult. One approach could be of using skip list, one could be traversing the linked list using one pointer.
Prerequisite : Finding middle of a linked list.
Note: The approach and implementation provided below are to show how Binary Search can be implemented on a linked list. The implementation takes O(n) time.
- Here, start node(set to Head of list), and the last node(set to NULL initially) are given.
- Middle is calculated using two pointers approach.
- If middle’s data matches the required value of search, return it.
- Else if middle’s data < value, move to upper half(setting start to middle's next).
- Else go to lower half(setting last to middle).
- The condition to come out is, either element found or entire list is traversed. When entire list is traversed, last points to start i.e. last -> next == start.
In main function, function InsertAtHead inserts value at the beginning of linked list. Inserting such values(for sake of simplicity) so that the list created is sorted.
Input : Enter value to search : 7 Output : Found Input : Enter value to search : 12 Output : Not Found
Time Complexity : O(n)
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.
- Difference between Singly linked list and Doubly linked list
- Convert singly linked list into circular linked list
- Convert Singly Linked List to XOR Linked List
- Sum of the nodes of a Singly Linked List
- QuickSort on Singly Linked List
- Circular Singly Linked List | Insertion
- Insertion Sort for Singly Linked List
- Convert a Singly Linked List to an array
- Implement a stack using singly linked list
- Alternate Odd and Even Nodes in a Singly Linked List
- Product of the nodes of a Singly Linked List
- Alternating split of a given Singly Linked List | Set 1
- Difference between a Static Queue and a Singly Linked List
- Program to delete all even nodes from a Singly Linked List
- Various operations on Fibonacci nodes in a Singly Linked list
- Find middle of singly linked list Recursively
- Sum and Product of all Prime Nodes of a Singly Linked List
- Sum and Product of all Fibonacci Nodes of a Singly Linked List
- Probability of finding an element K in 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.