Open In App

JavaScript Program to Check if Element Exists in Linked List

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A given element exists in a linked list, the task is to check whether this number is present in the linked list or not. A linked list provides an efficient way to organize and manage data. We can employ various methods to check for the existence of an element in a linked list, including both the Iterative and Recursive approaches.

Examples:

Input: 3 -> 4 -> 1 -> 7 -> 5, 
Target: 1
Output: 1 exists in the linked list.

Input: 2 -> 7 -> 5 -> 5 -> 8 -> 4,
Target: 6
Output: 6 does not exist in the linked list.

Use the below approaches to check if an Element exists in the Linked List:

Using Iterative Approach

This method iterates through the linked list using a while loop, checking each node’s value against the target value. If a match is found, it returns true, indicating the target exists in the list. Otherwise, if the end of the list is reached without a match, it returns false, indicating the target does not exist in the list.

Example: The code below uses an Iterative Approach to Check Element Existence in a Linked List.

Javascript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    constructor() {
        this.head = null;
    }

    addNode(data) {
        const newNode = new Node(data);
        if (!this.head) this.head = newNode;
        else {
            let curr = this.head;
            while (curr.next) curr = curr.next;
            curr.next = newNode;
        }
    }

    elementExists(target) {
        let current = this.head;
        while (current) {
            if (current.data === target) {
                return true;
            }
            current = current.next;
        }
        return false;
    }
}

const linkedList = new LinkedList();
linkedList.addNode(3);
linkedList.addNode(1);
linkedList.addNode(4);
linkedList.addNode(2);

const target = 1;
const exists = linkedList.elementExists(target);
if (exists) {
    console.log(`${target} exists in the linked list.`);
} else {
    console.log(`${target} does not exist in the linked list.`);
}

Output
1 exists in the linked list.

Time Complexity: O(n)

Space Complexity: O(1)

Using Recursive Approach

In the recursive approach, to checking element existence in a linked list, we examine each node one by one. If we find the desired element, we return true, indicating that the target exists in the list. Otherwise, we continue recursively until we either find the element or exhaust all nodes. If we reach the end without finding the element, we return false, indicating that the target does not exist in the list.

Example: The code below uses Recursive Approach to Check Element Existence in a Linked List.

Javascript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    constructor() {
        this.head = null;
    }

    addNode(data) {
        const newNode = new Node(data);
        if (!this.head) {
            this.head = newNode;
        } else {
            let curr = this.head;
            while (curr.next) {
                curr = curr.next;
            }
            curr.next = newNode;
        }
    }

    elementExists(target, node = this.head) {
        if (!node) {
            return false;
        }
        if (node.data === target) {
            return true;
        }
        return this.elementExists(target, node.next);
    }
}

const linkedList = new LinkedList();
linkedList.addNode(7);
linkedList.addNode(5);
linkedList.addNode(4);
linkedList.addNode(3);

const target = 3;
const exists = linkedList.elementExists(target);
if (exists) {
    console.log(`${target} exists in the linked list.`);
} else {
    console.log(`${target} does not exist in the linked list.`);
}

Output
3 exists in the linked list.

Time Complexity: O(n)

Space Complexity: O(n)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads