
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Search Element in a Linked List using JavaScript
To search for an element in a linked list, we need to go through each node one by one and check if its value matches the target element. If we find the element, we return its position in the list. If not, we return a message saying the element is not found.
In this article, our task is to implement a JavaScript program that searches for a specific element in a linked list and returns its position if found, or a message if it is not present.
Example
Let's look at the below examples:
Linked list: 10 -> 20 -> 30 -> 40 -> null Input: 40 Output: Element found at index 3 Input: 10 Output: Element found at index 0 Input: null Output: Element not found
Approaches to Search an Element in a Linked List
Here, we will cover two approaches for searching an element in a linked list in JavaScript:
Pseudo Code
Below is the pseudo-code for searching an element in linked list
ALGORITHM SearchInLinkedList(list, x)
INPUT: list (Linked List), x (Target Element)
OUTPUT: true if x is found, false otherwise
1. Initialize current ? list.head
2. WHILE current ? null DO
a. IF current.data = x THEN
RETURN true
b. current ? current.next
3. END WHILE
4. RETURN false
Steps for Searching an Element in the Linked List
Below are the steps for searching an element in a linked list in JavaScript:
- Step 1: Define a Node class with two properties: value and next. The value property holds the data, and the next property holds a reference to the next node in the list.
- Step 2: Define a LinkedList class with three properties: head, tail, and length. The head points to the first node, the tail points to the last node, and length represents the number of nodes in the list.
- Step 3: Define an add method in the LinkedList class. This method takes a value as an argument and adds a new node with that value to the end of the linked list.
- Step 4: Define a remove method in the LinkedList class. This method takes a value as an argument and removes the first node with that value from the list.
- Step 5: Define a search method in the LinkedList class. This method takes a value as an argument and returns the first node containing that value, or null if the node is not found.
- Step 6: Define a reverse method in the LinkedList class that reverses the order of the nodes in the linked list.
Iterative Search in a Linked List using JavaScript
The program defines a Node class to represent each element and a LinkedList class to manage the list. The add method adds new elements, and the search method iterates through the list to find the target element, returning its index if found or a message if not.
Example
This example shows how to add elements to a linked list and search for specific values efficiently.
// Define the Node class for a singly linked list class Node { constructor(data) { this.data = data; this.next = null; } } // Define the LinkedList class class LinkedList { constructor() { this.head = null; this.size = 0; } // Add an element to the linked list add(element) { const node = new Node(element); // If the linked list is empty, set the new node as the head if (this.head === null) { this.head = node; } else { // Traverse to the end of the linked list and add the new node let current = this.head; while (current.next !== null) { current = current.next; } current.next = node; } this.size++; } // Search for an element in the linked list search(element) { let current = this.head; let index = 0; // Traverse through the linked list until the element is found while (current !== null) { if (current.data === element) { return `Element found at index ${index}`; } current = current.next; index++; } return "Element not found"; } } // Create a new linked list const ll = new LinkedList(); // Add elements to the linked list ll.add(10); ll.add(20); ll.add(30); ll.add(40); ll.add(50); // Search for an element in the linked list const result = ll.search(30); console.log(result);
Output
Element found at index 2
Time Complexity: O(n), where n is the number of nodes in the list.
Space Complexity: O(1), as no extra space is used other than pointers for traversal.
Recursive Search in a Linked List using JavaScript
This program defines a search method that uses recursion to traverse the list, checking each node for the target element. If the element is found, it returns the index; otherwise, it moves to the next node and continues until the end of the list, returning a message if the element is not found.
Example
This example shows how to add elements to a linked list and search for specific values efficiently using recursion.
// Define the Node class for a singly linked list class Node { constructor(data) { this.data = data; this.next = null; } } // Define the LinkedList class class LinkedList { constructor() { this.head = null; this.size = 0; } // Add an element to the linked list add(element) { const node = new Node(element); // If the linked list is empty, set the new node as the head if (this.head === null) { this.head = node; } else { // Traverse to the end of the linked list and add the new node let current = this.head; while (current.next !== null) { current = current.next; } current.next = node; } this.size++; } // Recursively search for an element in the linked list searchRecursive(current, element, index = 0) { if (current === null) { return "Element not found"; } if (current.data === element) { return `Element found at index ${index}`; } return this.searchRecursive(current.next, element, index + 1); } // Wrapper method for the recursive search search(element) { return this.searchRecursive(this.head, element); } } // Create a new linked list const ll = new LinkedList(); // Add elements to the linked list ll.add(10); ll.add(20); ll.add(30); ll.add(40); ll.add(50); // Search for an element in the linked list const result = ll.search(30); console.log(result); // Output: Element found at index 2
Output
Element found at index 2
Time Complexity: O(n), where n is the number of nodes in the list.
Space Complexity: O(n),as each recursive call adds a frame to the call stack, proportional to the number of nodes in the list.
Complexity Comparison
Here is a comparison of the time and space complexity for both iterative and recursive approaches:
Approach | Time Complexity | Space Complexity |
---|---|---|
Iterative Search | O(n) | O(1) |
Recursive Search | O(n) | O(n) (due to call stack) |
Conclusion
In this article, we learned how to search for an element in a linked list using JavaScript. The program uses a LinkedList class to add elements and search through them. We covered both iterative and recursive approaches, where the search checks each node until the element is found or the end of the list is reached. If the element is found, the index is returned; if not, a "not found" message is shown.
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.