# Search in a Linked List

Posted: 10 Dec, 2020

Difficulty: Easy

#### You are given a Singly Linked List of integers with a head pointer. Every node of the Linked List has a value written on it.

#### A sample Linked List

#### Now you have been given an integer value 'K'. Your task is to check whether a node having a value equal to 'K' exists in the given linked list or not.

##### Input Format:

```
The first line of the input contains an integer 'T' representing the number of test cases or queries to be processed. Then the 'T' test case follows.
The first line of each test case contains space-separated integers denoting the values of nodes of the Linked List. The Linked List is terminated with -1. Hence, -1 is never a node value of the Linked List.
The third line of each test case contains a single integer 'K' which is desired to be checked in the Linked List.
For more clarity, please refer to the sample inputs.
```

##### Output Format:

```
For each test case, print 1 if the desired value 'K' exists in the Linked List; otherwise, print 0.
Print the answer for each test case in a new line.
```

##### Note:

```
You do not need to print anything, and it has already been taken care of. Just implement the given function.
```

##### Constraints:

```
1 <= T <= 10
1 <= L <= 10^5
1 <= data <= 10^9 and data != -1
1 <= K <= 10^9
Where 'T' represents the number of test cases, 'L' represents the total number of nodes in the Linked List, "data" represents the value at each node, and 'K' is the given integer.
Time Limit: 1 sec.
```

Approach 1

Let's try to build a recursive solution to this problem

The recursive function has three cases:

- The Head is NULL(it means that we have reached the end of the linked list without finding value ‘K’ that means the particular value is not present in the Linked List. So, in this case, we will return 0 as our answer).
- The Head is Not NULL, and the value of the head is equal to the desired value ‘K’ we will return 1 in this case as we found the desired value in the Linked List.
- The Head is Not NULL, and the current value is not equal to the desired value ‘K’, so it will recur to the next node of the head and undergo the same process there.

Approach 2

The idea is to first initialize a pointer pointing to the first element of the Linked List and will check if the pointer's data is equal to the desired value K. If yes, then we will return 1; otherwise, we will move the pointer ahead by 1 position. If at any point of time during our travel, the pointer gets to point the NULL node, i.e., the end of Linked List, we will return 0 (as we have reached the end).

SIMILAR PROBLEMS

# Ninja And The List

Posted: 19 Jan, 2022

Difficulty: Moderate

# Insertion In Circular Linked List

Posted: 21 Apr, 2022

Difficulty: Easy

# Insertion In A Singly Linked List

Posted: 22 Apr, 2022

Difficulty: Easy

# Deletion In Doubly Linked List

Posted: 22 Apr, 2022

Difficulty: Easy

# Insertion In Doubly Linked List

Posted: 24 Apr, 2022

Difficulty: Easy