Remove elements from a linked list using Javascript

Removing an element from a linked list involves breaking the connections between nodes. There are three main scenarios to handle based on the position of the element to be removed.

Three Cases for Element Removal

  • Removing from head: Simply assign head = head.next to lose reference to the first element.
  • Removing from tail: Set the second-to-last node's next property to null.
  • Removing from middle: Connect the previous node directly to the node after the one being removed: prevNode.next = nodeToRemove.next.

Visual Illustration

Original List: 10 20 30 40 After removing node with value 30: 10 20 40 New connection Node removed

Implementation

Here's a complete implementation of the remove method for a linked list:

// Node class for linked list
class ListNode {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

// LinkedList class with remove method
class LinkedList {
    constructor() {
        this.head = null;
        this.length = 0;
    }

    // Insert method for testing
    insert(data, position = this.length) {
        const newNode = new ListNode(data);
        
        if (position === 0 || !this.head) {
            newNode.next = this.head;
            this.head = newNode;
        } else {
            let current = this.head;
            for (let i = 0; i < position - 1 && current.next; i++) {
                current = current.next;
            }
            newNode.next = current.next;
            current.next = newNode;
        }
        this.length++;
    }

    // Remove method implementation
    remove(position = 0) {
        if (this.length === 0) {
            console.log("List is already empty");
            return;
        }

        let removedData;

        // Case 1: Remove from head
        if (position <= 0) {
            removedData = this.head.data;
            this.head = this.head.next;
        }
        // Case 2: Remove from tail or beyond
        else if (position >= this.length - 1) {
            if (this.length === 1) {
                removedData = this.head.data;
                this.head = null;
            } else {
                let current = this.head;
                while (current.next.next != null) {
                    current = current.next;
                }
                removedData = current.next.data;
                current.next = null;
            }
        }
        // Case 3: Remove from middle
        else {
            let current = this.head;
            for (let i = 0; i < position - 1; i++) {
                current = current.next;
            }
            removedData = current.next.data;
            current.next = current.next.next;
        }

        this.length--;
        return removedData;
    }

    // Display method for testing
    display() {
        if (!this.head) {
            console.log("List is empty");
            return;
        }
        
        let result = "";
        let current = this.head;
        while (current) {
            result += current.data + " -> ";
            current = current.next;
        }
        console.log(result + "null");
    }
}

// Testing the remove functionality
let list = new LinkedList();
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);

console.log("Original list:");
list.display();

console.log("\nRemoving element at position 2 (value 30):");
list.remove(2);
list.display();

console.log("\nRemoving head element:");
list.remove(0);
list.display();

console.log("\nRemoving tail element:");
list.remove();
list.display();
Original list:
10 -> 20 -> 30 -> 40 -> null

Removing element at position 2 (value 30):
10 -> 20 -> 40 -> null

Removing head element:
20 -> 40 -> null

Removing tail element:
20 -> null

Key Points

  • Always check if the list is empty before attempting removal
  • Handle edge cases like removing from a single-node list
  • Update the length property after successful removal
  • Be careful with boundary conditions to avoid null pointer errors

Time Complexity

Operation Time Complexity Description
Remove from head O(1) Direct access to head node
Remove from tail O(n) Must traverse to second-to-last node
Remove from middle O(n) Must traverse to target position

Conclusion

Removing elements from a linked list requires careful handling of node connections and edge cases. The key is maintaining proper references between nodes while updating the list length accordingly.

Updated on: 2026-03-15T23:18:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements