# 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:

: For removing vowels from the starting of the linked list, move the**At starting of the linked list****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: 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**In between of the linked list****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: 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**At the end of the linked list****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:

`// 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 ` ` ` `// occurring continuously ` ` ` `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*

*filter_none*

**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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the middle of a given linked list in C and Java
- Program for n'th node from the end of a Linked List
- Write a function to get Nth node in a Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Detect loop in a linked list
- Write a function to delete a Linked List
- Write a function that counts the number of times a given int occurs in a Linked List
- Reverse a linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Write a function to get the intersection point of two Linked Lists
- Function to check if a singly linked list is palindrome
- The Great Tree-List Recursion Problem.
- Clone a linked list with next and random pointer | Set 1
- Memory efficient doubly linked list
- Given a linked list which is sorted, how will you insert in sorted way
- Merge two sorted linked lists
- Print reverse of a Linked List without actually reversing
- Remove duplicates from an unsorted linked list
- Remove duplicates from a sorted linked list
- Reverse a Doubly Linked List

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.