We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
Search a specific node in Circular Linked List
Last updated:
To search a circular linked list, start at the first node and compare each node's value with the key while moving along the ring. The search ends either at a match or when it has gone all the way round; since every node has then been checked once, the key is not in the list. The worst case is O(n), the best case O(1).
The program below keeps a reference to the last node, so the first node is last.next.
Example
Java program to find a node in a circular linked list
SearchinCLL.java
// Java program to search for a value in a circular linked list
public class SearchinCLL {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
// Function to search a specific value
static void search(Node last, int key) {
// Returns, if the list is empty
if (last == null) {
System.out.println("List is empty.");
return;
}
Node head = last.next;
Node curr = head;
// Traverse the list to find the key
while (curr != last) {
if (curr.data == key) {
// Key found
System.out.println("The key: " + key + " is found.");
return;
}
curr = curr.next;
}
// Check the last node
if (last.data == key) {
// Key found
System.out.println("The key: " + key + " is found.");
return;
}
// Key not found
System.out.println("The key: " + key + " is not found.");
}
// Print list
static void printList(Node last) {
if (last == null)
return;
Node head = last.next;
while (true) {
System.out.print(head.data + " ");
head = head.next;
if (head == last.next)
break;
}
System.out.println();
}
public static void main(String[] args) {
// Create a circular linked list
Node head = new Node(50);
head.next = new Node(20);
head.next.next = new Node(40);
head.next.next.next = new Node(10);
head.next.next.next.next = new Node(30);
head.next.next.next.next.next = head;
// search() and printList() expect a reference to the last node
Node last = head.next.next.next.next;
System.out.print("List value: ");
printList(last);
// Passing null (an empty list) and 40 as a key
search(null, 40);
// Passing 40 as a key which is present in the list
search(last, 40);
// Passing 90 as a key which is not present in the list
search(last, 90);
}
}
Output
List value: 50 20 40 10 30
List is empty.
The key: 40 is found.
The key: 90 is not found.
Frequently Asked Questions (FAQ)
Q1. How do you search for a value in a circular linked list?
SearchCircular.java
public class SearchCircular {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static int indexOf(Node head, int value) {
if (head == null) {
return -1;
}
Node current = head;
int index = 0;
do {
if (current.data == value) {
return index;
}
current = current.next;
index++;
} while (current != head);
return -1; // checked every node once
}
}
Q2. How does the search know the value is not present?
It stops when it returns to the starting node. At that point every node has been checked once, so the value is not in the list.
Q3. What is the time complexity of searching a circular linked list?
O(n) in the worst case and O(1) in the best case. It uses O(1) extra space.
Q4. Write a recursive search method for a circular linked list.
SearchRecursive.java
public class SearchRecursive {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static boolean contains(Node current, Node head, int value) {
if (current.data == value) {
return true;
}
if (current.next == head) {
return false; // back at the start
}
return contains(current.next, head, value);
}
// Call: head != null && contains(head, head, value)
}
Q5. How do you find all positions of a value?
FindAll.java
import java.util.ArrayList;
import java.util.List;
public class FindAll {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static List<Integer> findAll(Node head, int value) {
List<Integer> positions = new ArrayList<>();
if (head == null) {
return positions;
}
Node current = head;
int index = 0;
do {
if (current.data == value) {
positions.add(index);
}
current = current.next;
index++;
} while (current != head);
return positions;
}
}
Q6. Can you search starting from the middle of the list?
Yes. Because the list is circular, starting at any node and stopping when you come back to it checks every node. This is useful when you already hold a reference to a recent position, such as the current song in a playlist.
Q7. How do you search in a sorted circular linked list efficiently?
Start from the smallest element (the head in a sorted circular list) and stop as soon as you pass a value larger than the target, because the value cannot appear later. The worst case is still O(n), but missing values are detected sooner.
Q8. What is a common bug in circular search?
Checking current.next != head before checking the current node, which skips the last node. Another bug is using while (current != head) without first moving off the head, which never enters the loop.
Q9. Write a Java method that returns the node before the node holding a value.
FindPrevious.java
public class FindPrevious {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static Node findPrevious(Node head, int value) {
if (head == null) {
return null;
}
Node previous = head;
do {
if (previous.next.data == value) {
return previous;
}
previous = previous.next;
} while (previous != head);
return null;
}
}
Finding the previous node is the first step of deleting a node in a singly circular list.
Q10. Why can binary search not be used on a circular linked list?
Binary search needs O(1) access to the middle element. In any linked list, reaching the middle means walking through the nodes, so binary search loses its O(log n) advantage.