JavaScript Program To Delete Alternate Nodes Of A Linked List

We will be writing a JavaScript program to delete alternate nodes of a linked list. We will be utilizing a while loop to traverse the linked list while keeping track of the current node. In each iteration, we will skip every second node by linking the current node directly to the node after the next one, effectively removing alternate nodes from the list.

Approach

  • Start traversing from the head node of the linked list.

  • For each node, check if both current node and its next node exist.

  • Skip the immediate next node by updating the current node's next pointer to point to the next-next node.

  • Move to the next remaining node (which is now the next node after skipping).

  • Repeat this process until reaching the end of the list.

  • Return the modified linked list with alternate nodes deleted.

Visual Representation

Before: 1 ? 2 ? 3 ? 4 ? 5 ? null 1 2 3 4 5 null After: 1 ? 3 ? 5 ? null 1 3 5 null Kept nodes Deleted nodes

Example

Here is a complete example of deleting alternate nodes of a linked list in JavaScript:

// Linked List Node
class Node {
   constructor(data) {
      this.data = data;
      this.next = null;
   }
}

// Linked List class
class LinkedList {
   constructor() {
      this.head = null;
   }
   
   // Method to add a node at the end
   append(data) {
      const newNode = new Node(data);
      if (!this.head) {
         this.head = newNode;
         return;
      }
      let current = this.head;
      while (current.next) {
         current = current.next;
      }
      current.next = newNode;
   }
   
   // Method to delete alternate nodes
   deleteAlternate() {
      let current = this.head;
      while (current !== null && current.next !== null) {
         // Skip the next node by connecting current to next.next
         current.next = current.next.next;
         current = current.next;
      }
   }
   
   // Method to print the linked list
   printList() {
      let result = [];
      let current = this.head;
      while (current !== null) {
         result.push(current.data);
         current = current.next;
      }
      console.log(result.join(' ? '));
   }
}

// Create a linked list
let list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.append(5);
list.append(6);

console.log("Linked List before deleting alternate nodes:");
list.printList();

list.deleteAlternate();

console.log("Linked List after deleting alternate nodes:");
list.printList();
Linked List before deleting alternate nodes:
1 ? 2 ? 3 ? 4 ? 5 ? 6
Linked List after deleting alternate nodes:
1 ? 3 ? 5

How It Works

The algorithm works by maintaining a pointer to the current node and checking if both the current node and its next node exist. When both conditions are met:

  • We update the current node's next pointer to skip the immediate next node and point directly to the next-next node.

  • This effectively removes the alternate node from the chain.

  • We then move the current pointer to the next remaining node.

  • The process continues until we reach the end of the list.

Time and Space Complexity

  • Time Complexity: O(n) where n is the number of nodes in the linked list.

  • Space Complexity: O(1) as we only use a constant amount of extra space.

Conclusion

Deleting alternate nodes from a linked list is efficiently achieved by traversing the list once and updating next pointers to skip every second node. This approach maintains the original structure while removing every alternate element in O(n) time.

Updated on: 2026-03-15T23:19:01+05:30

282 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements