# Searching an Element in a Linked List using C++

C++ProgrammingServer Side Programming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

To search an element in a linked list, we must iterate through the complete list, compare each node with the desired data, and keep searching until a match is obtained. Because a Linked List does not provide random access, we must start the search from the first node.

We are given a linked list of integers and an integer key. We need to find if this key exists in our linked list or not. We can do a simple linear search in the linked list and find the key. If present, we can return "Yes"; otherwise, "No"

Let us look at some input-output scenarios −

We have taken a list where we need to find whether the element is present in that list and get the output accordingly with the provided key as 3 −

Input Data: [ 10, 4, 5, 4, 10, 1, 3, 5]
Output: Yes


Let’s consider another scenario with the key provided as 5 −

Input Data: [ 1, 4, 9, 4, 10, 1, 3, 6]
Output: No


## Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Set up the head as null.

• Add few items to the Linked List

• Get the user's input for the item to be searched for.

• Linearly traverse the Linked List from head to end until you reach the null node.

• Check each node to see if the data value matches the item to be searched.

• Return the index of the node where the data was found. If not found, go to the next node.

## Example

For example, let's have a linked list such as "52->4651->42->5->12587->874->8->null" and its key is 12587. The C++ program to implement the example is given below −

#include <iostream>
using namespace std;
class Node {
public:
int val;
Node* next;
Node(int val) {
this->val = val;
next = NULL;
}
};
void solve(Node* head, int key) {
if(head->val == key) {
cout << "Yes";
return;
}
}
cout << "No";
}
int main() {
Node* head = new Node(52);
head->next = new Node(4651);
head->next->next = new Node(42);
head->next->next->next = new Node(5);
head->next->next->next->next = new Node(12587);
head->next->next->next->next->next = new Node(874);
head->next->next->next->next->next->next = new Node(8);
return 0;
}


## Output

Yes


## Example

Now we will use the recursive approach to solve the same problem −

#include <iostream>
using namespace std;
class Node {
public:
int val;
Node* next;
Node(int val) {
this->val = val;
next = NULL;
}
};
void solve(Node* head, int key) {
if(head == NULL) {
cout << "No";
} else if(head->val == key) {
cout << "Yes";
} else {
}
}
int main() {
Node* head = new Node(52);
head->next = new Node(4651);
head->next->next = new Node(42);
head->next->next->next = new Node(5);
head->next->next->next->next = new Node(12587);
head->next->next->next->next->next = new Node(874);
head->next->next->next->next->next->next = new Node(8);
return 0;
}


## Output

Yes


## Conclusion

The time complexity is O(n). We have used an iterative approach to solve this problem. Try this problem with a recursive approach.