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 preferred over arrays in case of deletions and insertions as they take O(1) time for the respective operations.
Advantages of Linked List:
- Insertions and deletions take O(1) time.
- Dynamic in nature.
- Memory is not contiguous.
Syntax:
LinkedList<ClassName> variableName = new LinkedList<>();
Example:
LinkedList<Integer> ll = new LinkedList<>();
Task:
Search for an element in a linked list.
Approach:
When the linked list is provided to us directly, we can use a for loop to traverse through the list and find the element. In case, if we are not allowed to use pre-built libraries, we need to create our very own linked list and search for the element.
Examples:
Input: ll1 = [10, 20, 30, -12, 0, 23, -2, 12]
element = 23
Output: 5
Input: ll2 = [1, 2, 3, 4, 5]
element = 3
Output: 2
The following are the two methods with which we can search for an element in a Linked List.
Method 1: When we are allowed to use in-built libraries
- First, a Linked list is initialized.
- A for loop is used to traverse through the elements present in the Linked List.
Below is the implementation of the above approach:
Java
import java.util.LinkedList;
class SearchInALinkedList {
public static void main(String[] args)
{
LinkedList<Integer> ll = new LinkedList<>();
ll.add( 1 );
ll.add( 2 );
ll.add( 3 );
ll.add( 4 );
ll.add( 5 );
ll.add( 6 );
ll.add( 7 );
int element = 4 ;
int ans = - 1 ;
for ( int i = 0 ; i < ll.size(); i++) {
int llElement = ll.get(i);
if (llElement == element) {
ans = i;
break ;
}
}
if (ans == - 1 ) {
System.out.println( "Element not found" );
}
else {
System.out.println(
"Element found in Linked List at " + ans);
}
}
}
|
OutputElement found in Linked List at 3
Time Complexity: O(n) where n is the number of elements present in the linked list.
Auxiliary Space: O(1)
Method 2: When we are not allowed to use in-built libraries
- First, create a generic node class.
- Create a LinkedList class and initialize the head node to null.
- Create the required add and search functions.
- Initialize the LinkedList in the main method.
- Use the search method to find the element.
Below is the implementation of the above approach:
Java
class Node<E> {
E data;
Node<E> next;
Node(E data) { this .data = data; }
}
class LinkedList<E> {
Node<E> head = null ;
int size = 0 ;
public void add(E element)
{
if (head == null ) {
head = new Node<>(element);
size++;
return ;
}
Node<E> add = new Node<>(element);
Node<E> temp = head;
while (temp.next != null ) {
temp = temp.next;
}
temp.next = add;
size++;
}
public int search(E element)
{
if (head == null ) {
return - 1 ;
}
int index = 0 ;
Node<E> temp = head;
while (temp != null ) {
if (temp.data == element) {
return index;
}
index++;
temp = temp.next;
}
return - 1 ;
}
}
public class GFG {
public static void main(String[] args) throws Exception
{
LinkedList<Integer> ll = new LinkedList<>();
ll.add( 1 );
ll.add( 10 );
ll.add( 12 );
ll.add(- 1 );
ll.add( 0 );
ll.add(- 19 );
ll.add( 34 );
int element = - 1 ;
int ans = ll.search(- 1 );
if (ans == - 1 ) {
System.out.println(
"Element not found in the Linked List" );
}
else
System.out.println(
"Element found in the Linked List at "
+ ans);
}
}
|
OutputElement found in the Linked List at 3
Time Complexity: O(n) where n is the number of elements present in the linked list.
Auxiliary Space: O(1)