Challenge: Search in Singly Linked List

This lesson explains how searching is done in a Singly Linked List. Also, there is a coding exercise in the end to test your concepts.

Introduction

Now, we will take a look at the second most important operation, i.e. searching for an element in a linked list. This exercise of finding a value in a Singly Linked List is relatively more straightforward than the other three operations of insertion.

Searching For A Node

The steps to search for a node in a Singly Linked List are as follows:

  1. Start from the headNode.
  2. Traverse the list till you either find a node with the given value, or you reach the end showing that the node being searched doesn’t exist.

Problem Statement

If you know how to insert an element in a list, then searching for an item will not be that difficult for you. Now, based on the steps mentioned above, we have designed a simple coding exercise for your practice. There is a solution placed in the solution section for your help, but try to solve it on your own first.

Method Prototype

boolean searchNode(SinglyLinkedList linkedlist, int value)

Output

It returns true or false to show if a certain value does or does not exists in the list.

Sample Input

linkedlist = 5->90->10->4   and  value = 4

Sample Output

true

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.