Java Program For Searching An Element In A Linked List
Last Updated :
15 Jun, 2022
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 function should return false. If key to be searched is 14, then the function should return true.
Iterative Solution:
1) Initialize a node pointer, current = head.
2) Do following while current is not NULL
a) current->key is equal to the key being searched return true.
b) current = current->next
3) Return false
Following is iterative implementation of above algorithm to search a given key.
Java
// Iterative Java program to search
// an element in linked list
//Node class
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
//Linked list class
class LinkedList
{
// Head of list
Node head;
// Inserts a new node at the front
// of the list
public void push(int new_data)
{
//Allocate new node and putting data
Node new_node = new Node(new_data);
//Make next of new node as head
new_node.next = head;
//Move the head to point to new Node
head = new_node;
}
// Checks whether the value x is present
// in linked list
public boolean search(Node head, int x)
{
// Initialize current
Node current = head;
while (current != null)
{
// Data found
if (current.data == x)
return true;
current = current.next;
}
// Data not found
return false;
}
// Driver code
public static void main(String args[])
{
// Start with the empty list
LinkedList llist = new LinkedList();
// Use push() to construct list
// 14->21->11->30->10
llist.push(10);
llist.push(30);
llist.push(11);
llist.push(21);
llist.push(14);
if (llist.search(llist.head, 21))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Pratik Agarwal
Output:
Yes
Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Recursive Solution:
bool search(head, x)
1) If head is NULL, return false.
2) If head's key is same as x, return true;
3) Else return search(head->next, x)
Following is the recursive implementation of the above algorithm to search a given key.
Java
// Recursive Java program to search an element
// in linked list
// Node class
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
// Linked list class
class LinkedList
{
// Head of list
Node head;
// Inserts a new node at the
// front of the list
public void push(int new_data)
{
// Allocate new node and putting data
Node new_node = new Node(new_data);
// Make next of new node as head
new_node.next = head;
// Move the head to point to new Node
head = new_node;
}
// Checks whether the value x is present
// in linked list
public boolean search(Node head, int x)
{
// Base case
if (head == null)
return false;
// If key is present in current node,
// return true
if (head.data == x)
return true;
// Recur for remaining list
return search(head.next, x);
}
// Driver code
public static void main(String args[])
{
// Start with the empty list
LinkedList llist = new LinkedList();
// Use push() to construct list
// 14->21->11->30->10
llist.push(10);
llist.push(30);
llist.push(11);
llist.push(21);
llist.push(14);
if (llist.search(llist.head, 21))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Pratik Agarwal
Output:
Yes
Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(n), for recursive call stack where n represents the length of the given linked list.
Please refer complete article on Search an element in a Linked List (Iterative and Recursive) for more details!
Similar Reads
LinkedList element() Method in Java In Java, the element() method of the LinkedList class is used to retrieve the first element in the list without removing it. The first element of the LinkedList is known as the head. Example 1: Here, we use the element() method to retrieve the first element of the LinkedList of Integers, without rem
2 min read
Java Program to Convert ArrayList to LinkedList Given an array list, your task is to write a program to convert the given array list to Linked List in Java. Examples: Input: ArrayList: [Geeks, forGeeks, A computer Portal] Output: LinkedList: [Geeks, forGeeks, A computer Portal] Input: ArrayList: [1, 2, 3, 4, 5] Output: LinkedList: [1, 2, 3, 4, 5]
6 min read
LinkedList getFirst() Method in Java Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and addr
2 min read
LinkedList get() Method in Java In Java, the get() method of LinkedList is used to fetch or retrieve an element at a specific index from a LinkedList.Example 1: Here, we use the get() method to retrieve an element at a specified index.Java// Java Program to illustrate get() method import java.util.LinkedList; public class Geeks {
2 min read
Circular Linked List Implementation in Java A Circular Linked List is a variation of the traditional linked list data structure. In the traditional linked list, the last node points to the null and it can indicating the end of the list. However, in the circular linked list, the last node points back to the first node and forms the circle or l
7 min read
ArrayList vs LinkedList in Java An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. However, the limitation of the array is that the size of the array is predefined and fixed. There are multiple ways to solve this problem. In this article, the diff
5 min read