Program to remove vowels from Linked List

Given a singly linked list, the task is to remove the vowels from the given linked list.

Examples:

Input: g -> e -> e -> k -> s -> f -> o -> r -> g -> e -> e -> k -> s
Output: g -> k -> s -> f -> r -> g -> k -> s
Explanation:
After removing vowels {e, e}, {o}, {e, e}. The linked list becomes
g -> k -> s -> f -> r -> g -> k -> s



Input: a -> a -> a -> g -> e -> e -> k -> s -> i -> i -> i -> m
Output: g -> k -> s -> m
Explanation:
After removing vowels {a, a, a}, {e, e}, {i, i, i}. The linked list becomes
g -> k -> s -> f -> r -> g -> k -> s

Approach:
There are three cases where we can find the vowels in the given linked list:

  1. At starting of the linked list: For removing vowels from the starting of the linked list, move the head Node to the first consonant occurs in the linked list.
    For Example:

    For Linked List:
    a -> e -> i -> a -> c -> r -> d -> NULL
    After moving the head Node from Node a to Node c,  We have 
    c -> r -> d -> NULL
    
  2. In between of the linked list: For removing vowels from the between of the linked list, the idea is to keep a marker of the last consonant found in the linked list before the vowel Nodes and change the next link of that Node with the next consonant Node found in the linked list after vowel Nodes.
    For Example:

    For Linked List:
    c -> r -> d -> a -> e -> i -> a -> c -> r -> z -> NULL
    last consonant before vowels {a, e, i, a} is d
    and next consonant after vowels {a, e, i, a} is r
    After linking next pointer of Node d to Node r, We have 
    c -> r -> d -> r -> z -> NULL
    
  3. At the end of the linked list: For removing vowels from the end of the linked list, the idea is to keep a marker of the last consonant found in the linked list before the vowel Nodes and change the next link of that Node with NULL.
    For Example:

    For Linked List:
    c -> r -> d -> a -> e -> i -> a -> NULL
    last consonant before vowels {a, e, i, a} is d
    After changing the next link of Node  to NULL, We have 
    c -> r -> d -> NULL
    

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to remove vowels
// Nodes in a linked list
#include <bits/stdc++.h>
using namespace std;
  
// A linked list node
struct Node {
    char data;
    struct Node* next;
};
  
// Head Node
struct Node* head;
  
// Function to add new node to the
// List
Node* newNode(char key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->next = NULL;
    return temp;
}
  
// Utility function to print the
// linked list
void printlist(Node* head)
{
    if (!head) {
        cout << "Empty List\n";
        return;
    }
    while (head != NULL) {
        cout << head->data << " ";
        if (head->next)
            cout << "-> ";
        head = head->next;
    }
    cout << endl;
}
  
// Utility function for checking vowel
bool isVowel(char x)
{
    return (x == 'a' || x == 'e' || x == 'i'
            || x == 'o' || x == 'u' || x == 'A'
            || x == 'E' || x == 'I' || x == 'O'
            || x == 'U');
}
  
// Function to remove the vowels Node
void removeVowels()
{
    // Node pointing to head Node
    struct Node* ptr = head;
  
    // Case 1 : Remove the trailing
    // vowels
    while (ptr != NULL) {
  
        // If current Node is a vowel
        // node then move the pointer
        // to next node
        if (isVowel(ptr->data))
            ptr = ptr->next;
  
        // Else break if a consonant
        // node is found
        else
            break;
    }
  
    // This prev node used to link
    // prev consonant to next
    // consonant after vowels
    struct Node* prev = ptr;
  
    // Head points to the first
    // consonant of the linked list
    head = ptr;
  
    ptr = ptr->next;
  
    // Case 2: If vowels found in
    // between of the linked list
    while (ptr != NULL) {
  
        // If current node is vowel
        if (isVowel(ptr->data)) {
  
            // Move ptr to the next
            // node
            ptr = ptr->next;
  
            // Check for vowels
            // occuring continously
            while (ptr != NULL) {
  
                // If ptr is a vowel
                // move to next pointer
                if (isVowel(ptr->data)) {
                    ptr = ptr->next;
                }
                // Else break if
                // consonant found
                else
                    break;
            }
  
            // Case 3: If we have ending
            // vowels then link the prev
            // consonant to NULL
            if (ptr == NULL) {
                prev->next = NULL;
                break;
            }
  
            // Case 2: change the next
            // link of prev to current
            // consonant pointing to
            // ptr
            else {
                prev->next = ptr;
            }
        }
  
        // Move prev and ptr to next
        // for next iteration
        prev = prev->next;
        ptr = ptr->next;
    }
}
  
// Driver code
int main()
{
    // Initialise the Linked List
    head = newNode('a');
    head->next = newNode('b');
    head->next->next = newNode('c');
    head->next->next->next = newNode('e');
    head->next->next->next->next = newNode('f');
    head->next->next->next->next->next = newNode('g');
    head->next->next->next->next->next->next = newNode('i');
    head->next->next->next->next->next->next->next = newNode('o');
  
    // Print the given Linked List
    printf("Linked list before :\n");
    printlist(head);
  
    removeVowels();
  
    // Print the Linked List after
    // removing vowels
    printf("Linked list after :\n");
    printlist(head);
  
    return 0;
}

chevron_right


Output:

Linked list before :
a -> b -> c -> e -> f -> g -> i -> o 
Linked list after :
b -> c -> f -> g

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

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.