Search an element in a Linked List (original) (raw)
Last Updated : 21 Oct, 2025
Given a **head of linked list and a key, determine whether the key exists by traversing through the nodes sequentially.
**Examples:
**Input: key = 5
**Output: true
**Explanation: 5 is present in the linked list.**Input: key = 13
**Output: true
**Explanation: node in the linked list has value = 13.**Input: key = 27
**Output: false
**Explanation: 27 is not present in the linked list.
Table of Content
- [Approach 1] Iterative Approach - O(n) Time and O(1) Space
- [Approach 2] Recursive Approach - O(n) Time and O(n) Space
[Approach 1] Iterative Approach - O(n) Time and O(1) Space
The idea is to traverse all the nodes of the linked list, starting from the **head. While traversing, if we find a node whose value is equal to **key then print "Yes", otherwise print "No".
**Step by Step Approach
- Initialize a node pointer, **curr = head.
- Do following while current is not NULL
=> If the current value (i.e., **curr->key) is equal to the key being searched return true.
=> Otherwise, move to the next node (**curr = curr->next). - If the key is not found, return false C++ `
#include using namespace std;
// a linked list node class Node { public: int data; Node* next;
// constructor to initialize a new node with data
Node(int x) {
data = x;
next = nullptr;
}};
// checks whether key is present in linked list bool searchKey(Node* head, int key) {
// initialize curr with the head of linked list
Node* curr = head;
// iterate over all the nodes
while (curr != NULL) {
// If the current node's value is equal to key,
// return true
if (curr->data == key)
return true;
// move to the next node
curr = curr->next;
}
// if there is no node with value as key, return false
return false;}
int main() {
// create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
// key to search in the linked list
int key = 5;
if (searchKey(head, key))
cout << "true";
else
cout << "false";
return 0;}
C
#include <stdbool.h> #include <stdio.h> #include <stdlib.h>
// Define a linked list node struct Node { int data; struct Node *next; };
// Function to create a new node struct Node *createNode(int x) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = x; newNode->next = NULL; return newNode; }
// Function to search for a key in the linked list bool searchKey(struct Node *head, int key) { struct Node *curr = head;
while (curr != NULL)
{
if (curr->data == key)
return true;
curr = curr->next;
}
return false;}
int main() { // Create a linked list: 1 -> 2 -> 3 -> 4 -> 5 struct Node *head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); head->next->next->next = createNode(4); head->next->next->next->next = createNode(5);
int key = 5;
if (searchKey(head, key))
printf("true\n");
else
printf("false\n");
return 0;}
Java
// a Linked List Node class Node { int data; Node next;
// constructor to initialize a new node with data
Node(int x) {
data = x;
next = null;
}}
public class GFG {
// checks whether key is present in linked list
static boolean searchKey(Node head, int key) {
// initialize curr with the head of linked list
Node curr = head;
// iterate over all the nodes
while (curr != null) {
// if the current node's value is equal to key,
// return true
if (curr.data == key)
return true;
// Move to the next node
curr = curr.next;
}
// if there is no node with value as key, return
// false
return false;
}
public static void main(String[] args) {
// create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
// key to search in the linked list
int key = 5;
if (searchKey(head, key))
System.out.println("true");
else
System.out.println("false");
}}
Python
a Linked List Node
class Node:
# constructor to intialize a node with data
def __init__(self, x):
self.data = x
self.next = Nonechecks whether key is present in linked list
def search_key(head, key):
# initialize curr with the head of linked list
curr = head
# iterate over all the nodes
while curr is not None:
# if the current node's value is equal to key,
# return true
if curr.data == key:
return True
# move to the next node
curr = curr.next
# if there is no node with value as key, return false
return Falseif name == "main":
# create a hard-coded linked list:
# 1 -> 2 -> 3 -> 4 -> 5
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
# key to search in the linked list
key = 5
if search_key(head, key):
print("true")
else:
print("false")C#
using System;
// a Linked List Node class Node { public int data; public Node next;
// constructor to initialize a new node with data
public Node(int x) {
data = x;
next = null;
}}
class GFG {
// checks whether key is present in linked list
static bool searchKey(Node head, int key) {
// initialize curr with the head of linked list
Node curr = head;
// iterate over all the nodes
while (curr != null) {
// if the current node's value is equal to key,
// return true
if (curr.data == key)
return true;
// move to the next node
curr = curr.next;
}
// if there is no node with value as key, return
// false
return false;
}
static void Main() {
// create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
// key to search in the linked list
int key = 5;
if (searchKey(head, key))
Console.WriteLine("true");
else
Console.WriteLine("false");
}}
JavaScript
class Node {
// constructor to initialize a new node with data
constructor(x) {
this.data = x;
this.next = null;
}}
// checks whether key is present in linked list function searchKey(head, key) {
// initialize curr with the head of linked list
let curr = head;
// iterate over all the nodes
while (curr !== null) {
// if the current node's value is equal to key,
// return true
if (curr.data === key)
return true;
// move to the next node
curr = curr.next;
}
// if there is no node with value as key, return false
return false;}
// Driver code
// create a hard-coded linked list: // 1 -> 2 -> 3 -> 4 -> 5 let head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5);
// key to search in the linked list let key = 5;
if (searchKey(head, key)) console.log("true"); else console.log("false");
`
[Approach 2] Recursive Approach - O(n) Time and O(n) Space
The idea is to recursively traverse all the nodes starting from the **head of linked list. For any node, if the value is equal to **key, then return true. Otherwise, recursively search the next node. If at any point the head reaches **NULL, it means that we have reached the end of linked list so return **false.
**Step by Step Approach
- If the head is NULL, return false.
- If the head's key is the same as **X, return true;
- Else recursively search in the next node. C++ `
#include using namespace std;
// Linked List Node class Node { public: int data; Node* next;
// constructor to initialize a new node with data
Node(int x) {
data = x;
next = nullptr;
}};
// checks whether the key is present in linked list bool searchKey(Node* head, int key) {
// base case
if (head == nullptr)
return false;
// if key is present in current node, return true
if (head->data == key)
return true;
// recur for remaining list
return searchKey(head->next, key);}
int main() {
// create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
// Key to search in the linked list
int key = 5;
if (searchKey(head, key))
cout << "true";
else
cout << "false";
return 0;}
C
#include <stdbool.h> #include <stdio.h> #include <stdlib.h>
// Linked list node struct Node { int data; struct Node *next; };
// Function to create a new node struct Node *createNode(int x) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = x; newNode->next = NULL; return newNode; }
// Recursive function to search for a key in the linked list bool searchKey(struct Node *head, int key) { // Base case: list is empty if (head == NULL) return false;
// If key is found at current node
if (head->data == key)
return true;
// Recur for the rest of the list
return searchKey(head->next, key);}
int main() { // Create a hard-coded linked list: 1 -> 2 -> 3 -> 4 -> 5 struct Node *head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); head->next->next->next = createNode(4); head->next->next->next->next = createNode(5);
int key = 5;
if (searchKey(head, key))
printf("true\n");
else
printf("false\n");
return 0;}
Java
// a Linked List Node class Node { int data; Node next;
// constructor to initialize a new node with data
Node(int x) {
data = x;
next = null;
}}
public class GFG {
// checks whether the key is present in linked list
static boolean searchKey(Node head, int key) {
// base case
if (head == null)
return false;
// if key is present in current node, return true
if (head.data == key)
return true;
// recur for remaining list
return searchKey(head.next, key);
}
public static void main(String[] args) {
// create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
// Key to search in the linked list
int key = 14;
if (searchKey(head, key))
System.out.println("true");
else
System.out.println("false");
}}
Python
a Linked List Node
class Node:
# constructor to initialize a new node with data
def __init__(self, x):
self.data = x
self.next = Nonechecks whether the key is present in linked list
def searchKey(head, key):
# base case
if head is None:
return False
# if key is present in current node, return true
if head.data == key:
return True
# recur for remaining list
return searchKey(head.next, key)if name == "main":
# create a hard-coded linked list:
# 1 -> 2 -> 3 -> 4 -> 5
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
# key to search in the linked list
key = 5
if searchKey(head, key):
print("true")
else:
print("false")C#
using System;
// a Linked List Node class Node { public int data; public Node next;
// constructor to initialize a new node with data
public Node(int x) {
data = x;
next = null;
}}
// checks whether the key is present in linked list class GFG {
// checks whether the key is present in linked list
static bool searchKey(Node head, int key) {
// base case
if (head == null)
return false;
// if key is present in current node, return true
if (head.data == key)
return true;
// recur for remaining list
return searchKey(head.next, key);
}
static void Main() {
// create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
// key to search in the linked list
int key = 5;
if (searchKey(head, key))
Console.WriteLine("true");
else
Console.WriteLine("false");
}}
JavaScript
// a Linked List Node class Node {
// constructor to initialize a new node with data
constructor(x) {
this.data = x;
this.next = null;
}}
// checks whether the key is present in linked list function searchKey(head, key) {
// base case
if (head === null)
return false;
// if key is present in current node, return true
if (head.data === key)
return true;
// recur for remaining list
return searchKey(head.next, key);}
// Driver Code
// create a hard-coded linked list: // 1 -> 2 -> 3 -> 4 -> 5 let head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5);
// key to search in the linked list let key = 5;
if (searchKey(head, key)) console.log("true"); else console.log("false");
`


