Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 10 Dec, 2020

Easy

```
The first line of the input 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 second line contains a single integer 'K', which is desired to be checked in the Linked List.
```

```
The only line contains 1 if the desired value 'K' exists in the Linked List, otherwise, print 0.
```

```
You do not need to print anything; it has already been handled. Just implement the given function.
```

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.

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

Deletion In Doubly Linked List

Easy

Posted: 22 Apr, 2022

Deletion In Doubly Linked List

Easy

Posted: 22 Apr, 2022

Insertion In Doubly Linked List

Easy

Posted: 24 Apr, 2022

Insertion In Doubly Linked List

Easy

Posted: 24 Apr, 2022

LRU Cache

Moderate

Posted: 10 Sep, 2022

Delete Nodes On Regular Intervals

Ninja

Posted: 19 Dec, 2022

Add One To Linked List

Hard

Posted: 19 Dec, 2022