
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Binary Search on Singly Linked List in C++
In this article, we are given a sorted singly linked list, and our task is to search for a given node using a binary search algorithm. The binary search algorithm works on the divide-and-conquer principle as it keeps dividing the list in half before searching. To search for an element in the linked list using binary search, it should be sorted.
In the sorted linked list, we find the middle node using two pointers (slow and fast) and compare it with the target node, and based on the comparison, we either search in the left or right sub-list or return the middle node's value.
Following is a short animation of the working of a binary search in a singly linked list to search for the target node, where we are searching for the node with value 5:
Let us see some example scenarios:
Scenario 1
Input: list: 1 -> 3 -> 5 -> 7 -> 9 -> 12 -> 15 -> 18 -> 20, target: 12 Output: Element 12 found at index 5
Scenario 2
Input: list: 1 -> 3 -> 5 -> 7 -> 9 -> 12 -> 15 -> 18 -> 20, target: 13 Output: Element 13 not found
Steps to Implement Binary Search on a Singly Linked List
The steps to search the target node in a singly linked list using binary search are given below:
- Define a binarySearch() function that accepts the head of the linked list and the target value that you want to search.
- Set the start pointer to head and the end pointer to nullptr.
- Find the middle node using the getMiddle() function. This function uses two pointers, a slow pointer that moves one step, and a fast pointer that moves two steps until the fast pointer reaches the end of the linked list.
- Compare the target value with the middle node using an if-else statement and return the index using the getIndex() function if the target is found at the middle node.
- If the target is not found at the middle node, then check if it is greater than or less than the middle node.
- If the target is greater than the middle node's data, then set the start pointer to mid->next and search in the right half of the list.
- If the target is less than the middle node's data, then set the end pointer to mid and search in the left half of the list.
- Repeat the above steps until the start pointer and end pointer become equal. If the element is found, return the index; otherwise, return element not found.
Binary Search on a Singly Linked List in C++
Following is an example code implementing the steps mentioned above to search the node with value 5 in the given sorted linked list using binary search:
#include <iostream> using namespace std; // Node structure struct Node { int data; Node* next; }; // Function to create a new node Node* newNode(int value) { Node* temp = new Node; temp->data = value; temp->next = nullptr; return temp; } // Function to insert node at the end void append(Node** head, int value) { Node* temp = newNode(value); if (*head == nullptr) { *head = temp; return; } Node* current = *head; while (current->next != nullptr) current = current->next; current->next = temp; } // Function to find the middle node between start and end Node* getMiddle(Node* start, Node* end) { if (start == nullptr) return nullptr; Node* slow = start; Node* fast = start; while (fast != end && fast->next != end) { slow = slow->next; fast = fast->next->next; } return slow; } // Function to find the index of a node in the list int getIndex(Node* head, Node* target) { int index = 0; while (head != nullptr) { if (head == target) return index; head = head->next; index++; } return -1; } // Binary Search Function int binarySearch(Node* head, int target) { Node* start = head; Node* end = nullptr; while (start != end) { Node* mid = getMiddle(start, end); if (mid == nullptr) return -1; if (mid->data == target) return getIndex(head, mid); else if (mid->data < target) start = mid->next; else end = mid; } return -1; } // Function to print the linked list void printList(Node* head) { while (head != nullptr) { cout << head->data; if (head->next != nullptr) cout << " -> "; head = head->next; } cout << endl; } int main() { Node* head = nullptr; // Creating a sorted linked list int arr[] = {1, 3, 5, 7, 9, 11}; int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < n; i++) append(&head, arr[i]); cout << "Linked List: "; printList(head); int target = 5; int index = binarySearch(head, target); if (index != -1) cout << "Element " << target << " found at index " << index << endl; else cout << "Element " << target << " not found." << endl; return 0; }
The output of the above code is as follows:
Linked List: 1 -> 3 -> 5 -> 7 -> 9 -> 11 Element 5 found at index 2
It is better to use Linear Search on a sorted Singly Linked List as its time complexity is O(n) in the worst case and O(1) in the best case.