Open In App

Javascript Program For Searching An Element In A Linked List

Last Updated : 15 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Write a function that searches a given key ‘x’ in a given singly linked list. The function should return true if x is present in linked list and false otherwise.

bool search(Node *head, int x) 

For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then function should return false. If key to be searched is 14, then the function should return true.
Iterative Solution: 

1) Initialize a node pointer, current = head.
2) Do following while current is not NULL
    a) current->key is equal to the key being searched return true.
    b) current = current->next
3) Return false 

Following is iterative implementation of above algorithm to search a given key.

Javascript




<script>
// Iterative javascript program
// to search an element
// in linked list
 
//Node class
class Node
{
    constructor(d)
    {
        this.data = d;
        this.next = null;
    }
}
 
// Linked list class
 
// Head of list
var head;
 
// Inserts a new node at the front of the list
function push(new_data)
{
    // Allocate new node and putting data
    var new_node = new Node(new_data);
 
    // Make next of new node as head
    new_node.next = head;
 
    // Move the head to point to new Node
    head = new_node;
}
 
// Checks whether the value
// x is present in linked list
function search(head , x)
{
    // Initialize current
    var current = head;
         
    while (current != null)
    {
        if (current.data == x)
 
            // Data found
            return true;
            current = current.next;
        }
 
        // Data not found
        return false;
}
 
// Driver code
 
// Start with the empty list
// Use push() to construct list
// 14->21->11->30->10
push(10);
push(30);
push(11);
push(21);
push(14);
 
if (search(head, 21))
    document.write("Yes");
else
    document.write("No");
// This code contributed by aashish1995
</script>


Output: 

Yes

Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Recursive Solution: 

bool search(head, x)
1) If head is NULL, return false.
2) If head's key is same as x, return true;
3) Else return search(head->next, x) 

Following is the recursive implementation of the above algorithm to search a given key.

Javascript




<script>
// Recursive javascript program to search
// an element in linked list
 
// Node class
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}
  
// Linked list class
// Head of list
var head;
 
// Inserts a new node at the front
// of the list
function push(new_data)
{
    // Allocate new node and putting data
    var new_node = new Node(new_data);
 
    // Make next of new node as head
    new_node.next = head;
 
    // Move the head to point to new Node
    head = new_node;
}
 
// Checks whether the value x is present
// in linked list
function search(head, x)
{
    // Base case
    if (head == null)
        return false;
 
    // If key is present in current node,
    // return true
    if (head.data == x)
        return true;
 
    // Recur for remaining list
    return search(head.next, x);
}
 
// Driver code
     
// Start with the empty list
// Use push() to construct list
// 14->21->11->30->10
push(10);
push(30);
push(11);
push(21);
push(14);
 
if (search(head, 21))
    document.write("Yes");
        else
            document.write("No");
 
// This code contributed by gauravrajput1
</script>


Output:  

Yes

Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(n), for recursive stack where n represents the length of the given linked list.

Please refer complete article on Search an element in a Linked List (Iterative and Recursive) for more details!
 



Previous Article
Next Article

Similar Reads

C++ Program For Searching An Element In A Linked List
Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14-&gt;21-&gt;11-&gt;30-&gt;10, then function should return false. If key to be searched is 1
4 min read
C Program For Searching An Element In A Linked List
Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14-&gt;21-&gt;11-&gt;30-&gt;10, then function should return false. If key to be searched is 1
4 min read
Java Program For Searching An Element In A Linked List
Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14-&gt;21-&gt;11-&gt;30-&gt;10, then function should return false. If key to be searched is 1
4 min read
Python Program For Searching An Element In A Linked List
Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14-&gt;21-&gt;11-&gt;30-&gt;10, then function should return false. If key to be searched is 1
4 min read
Javascript Program To Merge A Linked List Into Another Linked List At Alternate Positions
Given two linked lists, insert nodes of the second list into the first list at alternate positions of the first list. For example, if first list is 5-&gt;7-&gt;17-&gt;13-&gt;11 and second is 12-&gt;10-&gt;2-&gt;4-&gt;6, the first list should become 5-&gt;12-&gt;7-&gt;10-&gt;17-&gt;2-&gt;13-&gt;4-&gt;11-&gt;6 and second list should become empty. The
3 min read
Searching in Circular Linked list
Given a Circular Linked List CList, and an element K, the task is to check if this element K is present in the Circular Linked list or not. Example: Input: CList = 6-&gt;5-&gt;4-&gt;3-&gt;2, K = 3 Output: Found Input: CList = 6-&gt;5-&gt;4-&gt;3-&gt;2, K = 1 Output: Not Found Approach: The approach to find an element in Circular Linked List can be
7 min read
Create new linked list from two given linked list with greater element at each node
Given two linked list of the same size, the task is to create a new linked list using those linked lists. The condition is that the greater node among both linked list will be added to the new linked list.Examples: Input: list1 = 5-&gt;2-&gt;3-&gt;8 list2 = 1-&gt;7-&gt;4-&gt;5 Output: New list = 5-&gt;7-&gt;4-&gt;8 Input: list1 = 2-&gt;8-&gt;9-&gt;
8 min read
Convert Singly Linked List into Circular Linked List using JavaScript
We are given a singly linked list and we need to convert it into a circular linked list, where the last node points back to the head of the list. Converting a singly linked list into a circular linked list involves making the last node's next pointer point back to the head, creating a circular structure. This transformation enables traversal from t
3 min read
C++ Program To Merge A Linked List Into Another Linked List At Alternate Positions
Given two linked lists, insert nodes of second list into first list at alternate positions of first list. For example, if first list is 5-&gt;7-&gt;17-&gt;13-&gt;11 and second is 12-&gt;10-&gt;2-&gt;4-&gt;6, the first list should become 5-&gt;12-&gt;7-&gt;10-&gt;17-&gt;2-&gt;13-&gt;4-&gt;11-&gt;6 and second list should become empty. The nodes of se
3 min read
C Program To Merge A Linked List Into Another Linked List At Alternate Positions
Given two linked lists, insert nodes of the second list into the first list at alternate positions of the first list. For example, if first list is 5-&gt;7-&gt;17-&gt;13-&gt;11 and second is 12-&gt;10-&gt;2-&gt;4-&gt;6, the first list should become 5-&gt;12-&gt;7-&gt;10-&gt;17-&gt;2-&gt;13-&gt;4-&gt;11-&gt;6 and second list should become empty. The
3 min read
Practice Tags :