Searching a singly linked list means looking for a node whose value equals a given key. Because there is no index to jump to, the only way is to check the nodes one after another from the head. That makes search O(n) in the worst case (the key is in the last node or absent) and O(1) in the best case (the key is in the head).
The search can be written in two ways:
- Iterative Approach
- Recursive Approach
Search a key in a linked list using Iterative approach
A loop walks a reference through the list. As soon as a node holds the key, the method reports success and stops; if the reference runs off the end (null) without a match, the key is absent.
// Java program to search a key in a linked list using Iterative approach
class Node {
int data;
Node next;
// Constructor
Node(int data) {
this.data = data;
this.next = null;
}
}
public class SearchUsingIterativeApproachInSLL {
// Prints whether the key is present in the linked list
static void searchKey(Node head, int key) {
// Initialize tempNode with the head of linked list
Node tempNode = head;
// Iterate over all the nodes
while (tempNode != null) {
// Key is present, if tempNode's data is equal to the key.
if (tempNode.data == key) {
System.out.println(key + " is PRESENT in the list.");
return; // returning because key is found.
}
// Move to the next node
tempNode = tempNode.next;
}
// Key is not present, if there is no node with data as key
System.out.println(key + " is NOT PRESENT in the list.");
}
public static void main(String[] args) {
// Create a linked list
Node head = new Node(50);
head.next = new Node(30);
head.next.next = new Node(10);
head.next.next.next = new Node(70);
head.next.next.next.next = new Node(40);
// Linked list will be created like this
// 50 -> 30 -> 10 -> 70 -> 40
// 10 is the Key to search in the linked list
searchKey(head, 10);
// 100 is the Key to search in the linked list
searchKey(head, 100);
}
}
10 is PRESENT in the list. 100 is NOT PRESENT in the list.
Time complexity: O(n) in the worst case for a list of n nodes; O(1) if the key is in the head.
Extra space: O(1); only one reference variable is used.
Search a key in a linked list using Recursive approach
The recursive version expresses the same idea as a rule: an empty list (null) cannot contain the key; a list whose first node holds the key does contain it; otherwise the answer is whatever the search of the rest of the list (head.next) returns.
// Java program to search a key in a linked list using Recursive approach
class Node {
int data;
Node next;
// Constructor
Node(int data) {
this.data = data;
this.next = null;
}
}
public class SearchUsingRecursiveApproachInSLL {
// Returns true if the key is present in the list
static boolean searchKey(Node head, int key) {
// If head is equal to null that means list is empty
if (head == null)
return false;
// If key is present in the current node
if (head.data == key)
return true;
// Search the rest of the list
return searchKey(head.next, key);
}
public static void main(String[] args) {
// Create a linked list
Node head = new Node(50);
head.next = new Node(30);
head.next.next = new Node(10);
head.next.next.next = new Node(70);
head.next.next.next.next = new Node(40);
// Linked list will be created like this
// 50 -> 30 -> 10 -> 70 -> 40
// Scenario: Key is present
if (searchKey(head, 70))
System.out.println("Key 70 is PRESENT in the list.");
else
System.out.println("Key 70 is NOT PRESENT in the list.");
// Scenario: Key is not present
if (searchKey(head, 5))
System.out.println("Key 5 is PRESENT in the list.");
else
System.out.println("Key 5 is NOT PRESENT in the list.");
}
}
Key 70 is PRESENT in the list. Key 5 is NOT PRESENT in the list.
Time complexity: O(n) in the worst case, the same as the loop.
Extra space: O(n) in the worst case, because each recursive call adds a frame to the call stack.