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
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 to Search an Element in a Circular Linked List 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->
3 min read
Java Program to Get the First and the Last Element of a Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The given task is to retrieve the first and the last element of a given linked list. Properties of a Linked ListElements are stored in a non-contiguous manner.Every element is an object whi
4 min read
Java Program For Finding The Middle Element Of A Given Linked List Given a Singly linked list, find the middle of the linked list. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. Example of Finding Middle Element of Linked ListInput: 1->2->3->4->5 Output: 3Â Input: 1->2->3->4->5->
6 min read
Adding an Element to the Front of LinkedList in Java A Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements are linked using pointers and addresses. Each element is known as a node. This article shows how to add an element to the front of LinkedList in Java. Method 1: (Using user-def
3 min read
Java Program to Get Elements of a LinkedList Linked List is a linear data structure, in which the elements are not stored at the contiguous memory locations. Here, the task is to get the elements of a LinkedList. 1. We can use get(int variable) method to access an element from a specific index of LinkedList: In the given example, we have used
4 min read
Java Program to Implement LinkedList API Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data pa
10 min read
Java Program to Implement Triply Linked List Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. In this post, methods to insert a new node in a linked list are discussed. A node can be inserted in three ways either at the front of the linked list or after a given node or at the
8 min read
Java Program to Search an Element in Vector A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin
3 min read
Java Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15-
6 min read