Java Program to Search an Element in a Circular Linked List Last Updated : 01 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report A linked list is a kind of linear data structure where each node has a data part and an address part which points to the next node. A circular linked list is a type of linked list where the last node points to the first one, making a circle of nodes. Example: Input : CList = 6->5->4->3->2, find = 3 Output: Element is present Input : CList = 6->5->4->3->2, find = 1 Output: Element is not presentSearch an Element in a Circular Linked List For example, if the key to be searched is 30 and the linked list is 5->4->3->2, then the function should return false. If the key to be searched is 4, then the function should return true. Approach: Initialize a node pointer, temp = headInitialize a counter f=0 (to check if the element is present in a linked list or not)If the head is null then the print list is emptyElse start traversing the Linked List and if element found in Linked List increment in f.If the counter is zero, then the print element is not found Below is the implementation of the above approach: Java // Java program to Search an Element // in a Circular Linked List public class search { class Node { int data; Node next; public Node(int data) { this.data = data; } } // declaring head pointer as null public Node head = null; public Node tempo = null; // function adds new nodes at the end of list public void addNode(int data) { Node new1 = new Node(data); // If linked list is empty if (head == null) { head = new1; } else { tempo.next = new1; } tempo = new1; // last node points to first node tempo.next = head; } public void find(int key) { // temp will traverse the circular // linked list for searching element Node temp = head; // counter used to check if // element is found or not int f = 0; if (head == null) { System.out.println("List is empty"); } else { do { if (temp.data == key) { System.out.println( "element is present"); f = 1; break; } temp = temp.next; } while (temp != head); if (f == 0) { System.out.println( "element is not present"); } } } public static void main(String[] args) { search s = new search(); // Adds data to the list s.addNode(5); s.addNode(4); s.addNode(3); s.addNode(2); // Search for node 2 in the list s.find(2); // Search for node 6 in the list s.find(6); } } Outputelement is present element is not present Time Complexity: O(N), where N is the length of the Circular linked list. Space Complexity: O(1) because it is using constant space Comment More infoAdvertise with us Next Article Java Program For Searching An Element In A Linked List P pradiptamukherjee Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 circular linked list +1 More Practice Tags : circular linked listJava Similar Reads Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre 5 min read Java Program For Searching An Element In A Linked List Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functi 4 min read Java Program to Sort the Elements of the Circular Linked List In a circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list. Here, Create a circular linked list and sort the circular linked list in ascending order. Circular linked list before sorting: CIRCULAR LINKED LIST Circular linked li 3 min read Java Program To Recursively Linearly Search An Element In An Array Given an array arr[] of n elements, write a recursive function to search for a given element x in the given array arr[]. If the element is found, return its index otherwise, return -1.Input/Output Example:Input : arr[] = {25, 60, 18, 3, 10}, Element to be searched x = 3Output : 3 (index )Input : arr 3 min read Java Program to Delete a Node From the Ending of the Circular Linked List In this article, we will learn about deleting a node from the ending of a circular linked list. Consider the linked list as shown below:Â Example:Input : 5->3->4->(head node)Output: 5->3->(head node)We will first initialize the list and add some data into it with the addNode() method a 3 min read Java Program to Delete a Node From the Beginning of the Circular Linked List In this article, we will learn about deleting a node from the beginning of a circular linked list. Consider the linked list as shown below. Example: Input : 5->3->4->(head node) Output: 3->4->(head node) Two cases arrive while solving the problem, Case 1: List is empty If the list is 3 min read Like