Arrange consonants and vowels nodes in a linked list in C++?



A singly linked list is a fundamental data structure that consists of nodes, where each node contains a data field and a reference to the next node in the linked list. The next of the last node is null, indicating the end of the list.

The given task is to rearrange the linked list in such a way that all the vowel nodes precede the consonants while maintaining the order of their arrival.

Scenario

Input: a -> b-> c -> d-> e-> f-> g-> h-> i
Output: a-> e-> i-> b-> c-> d-> f-> g-> h

To solve this problem, we will use two dummy nodes to create two linked lists, one for vowels and the other for consonants. While traversing the given linked list, we will add vowel and consonant nodes to their respective lists. After the traversal, we will connect the vowel list to the consonant list.

Algorithm

Let's see the following algorithm to arrange the consonants and vowels nodes in a linked list:

  • Create two dummy nodes: one for vowels named vowelDummy and one for consonants named consonantDummy.

  • Initialize two tail pointers, vowelTail to vowelDumy and consonantTail to consonantDummy.

  • Set the pointer current to the head of the given linked list.

  • Traverse the list until the current is not null.

  • If the current node character is a vowel, append it to the vowel list and move vowelTail forward; otherwise, append it to the consonant list and move consonantTail forward.

  • Move the current to the next node.

  • After traversal, link the vowel list to the consonant list by using vowelTail -> next = consonantDummy -> next.

  • Set the consonantTail -> next to null to terminate the list.

  • Return vowelDummy -> next as the head of the rearranged linked list.

Example

Following is the program to arrange the consonants and vowels nodes in a linked list:

#include <iostream>
using namespace std;

// Define the structure for a linked list node
struct Node {
  char data;
  Node* next;
  Node(char val) : data(val), next(nullptr) {}
};
// Function to check whether a character is a vowel
bool isVowel(char ch) {
  ch = tolower(ch); 
  return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
// Function to rearrange the linked list
Node* rearrange(Node* head) {
  if (!head || !head->next) return head;

  // Dummy nodes for vowel and consonant lists
  Node* vowelDummy = new Node(0);
  Node* consonantDummy = new Node(0);
  
  // Tail pointers
  Node* vowelTail = vowelDummy;
  Node* consonantTail = consonantDummy;
  
  // Traverse the given list
  Node* current = head;
  while (current) {
    if (isVowel(current->data)) {
      vowelTail->next = current;
      vowelTail = vowelTail->next;
    } else {
      consonantTail->next = current;
      consonantTail = consonantTail->next;
    }
    current = current->next;
  }
  // Connect vowel list to consonant list
  vowelTail->next = consonantDummy->next;
  consonantTail->next = nullptr;
  // New head is the start of the vowel list
  Node* newHead = vowelDummy->next;
  //removing dummy nodes
  delete vowelDummy;
  delete consonantDummy;
  return newHead;
}
// Function to print the linked list
void printList(Node* head) {
  while (head) {
    cout << head->data << " -> ";
    head = head->next;
  }
  cout << "null" << endl;
}

int main() {
  // Create linked list: a -> b -> e -> d -> i
  Node* head = new Node('a');
  head->next = new Node('b');
  head->next->next = new Node('e');
  head->next->next->next = new Node('d');
  head->next->next->next->next = new Node('i');

  cout << "Given List:\n";
  printList(head);

  head = rearrange(head);

  cout << "Rearranged List (Vowels before consonants):\n";
  printList(head);

  return 0;
}

Following is the output of the above program:

Given List:
a -> b -> e -> d -> i -> null
Rearranged List (Vowels before consonants):
a -> e -> i -> b -> d -> null 
Updated on: 2025-08-06T11:10:56+05:30

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements