Javascript Program For Searching An Element In A Linked List (original) (raw)
Last Updated : 09 Sep, 2024
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:
- Initialize a node pointer, current = head.
- Do following while current is not NULL
a) current->key is equal to the key being searched return true.
b) current = current->next - Return false
Following is iterative implementation of above algorithm to search a given key.
JavaScript `
// 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 let head;
// Inserts a new node at the front of the list function push(new_data) { // Allocate new node and putting data let 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 let 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)) console.log("Yes"); else console.log("No"); // This code contributed by aashish1995
`
**Complexity Analysis:
- **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)
- If head is NULL, return false.
- If head's key is same as x, return true;
- Else return search(head->next, x)
Following is the recursive implementation of the above algorithm to search a given key.
JavaScript `
// 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 let head;
// Inserts a new node at the front // of the list function push(new_data) { // Allocate new node and putting data let 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)) console.log("Yes"); else console.log("No");
// This code contributed by gauravrajput1
`
Complexity Analysis:
- **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!
Similar Reads
- Learn Data Structures with Javascript | DSA using JavaScript Tutorial JavaScript (JS) is the most popular lightweight, interpreted programming language, and might be your first preference for Client-side as well as Server-side developments. But have you thought about using JavaScript for DSA? Learning Data Structures and Algorithms can be difficult when combined with 7 min read
- Learn Algorithms with Javascript | DSA using JavaScript Tutorial This Algorithms with Javascript tutorial is designed to help you understand and implement fundamental algorithms using the versatile JavaScript programming language. Whether you are a beginner in programming or looking to enhance your algorithmic skills, this guide will walk you through essential co 15+ min read