How to search an element in a Singly Linked List in Java ?

By | June 18, 2020

In this post, We will learn How to search an element in a Singly Linked List in Java?

Logic is Very Simple:

  1. If Linked List is Empty then search key will not be found so return false
  2. Traverse Linked List from head to last node(remember the last node always point to null) and compare Each node data with search key any of Node value and search key are equal then return true.
  3. if we do not get a search key while traversing throughout the Linked List(Step-2) then return false.

Below is the complete source code:

Output of this program:

Linked List is:
5->10->15->20->25->null
15 found in Linked List!!

You May Also Like:

That’s all about the How to search an element in a Singly Linked List in Java ?
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *