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:
- At the starting of the linked list: For removing vowels from the start of the linked list, move the head Node to the first consonant that 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
- In the 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
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
struct Node* next;
};
struct Node* head;
Node* newNode( char key)
{
Node* temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
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;
}
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' );
}
void removeVowels()
{
struct Node* ptr = head;
while (ptr != NULL) {
if (isVowel(ptr->data))
ptr = ptr->next;
else
break ;
}
struct Node* prev = ptr;
head = ptr;
ptr = ptr->next;
while (ptr != NULL) {
if (isVowel(ptr->data)) {
ptr = ptr->next;
while (ptr != NULL) {
if (isVowel(ptr->data)) {
ptr = ptr->next;
}
else
break ;
}
if (ptr == NULL) {
prev->next = NULL;
break ;
}
else {
prev->next = ptr;
}
}
prev = prev->next;
ptr = ptr->next;
}
}
int main()
{
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' );
printf ( "Linked list before :\n" );
printlist(head);
removeVowels();
printf ( "Linked list after :\n" );
printlist(head);
return 0;
}
|
Java
import java.io.*;
class Node
{
char data;
Node next;
Node( char item)
{
data = item;
next = null ;
}
}
class GFG
{
public static Node head;
static void printlist(Node head)
{
if (head == null )
{
System.out.println( "Empty List" );
}
while (head != null )
{
System.out.print(head.data + " " );
if (head.next != null )
{
System.out.print( "-> " );
}
head = head.next;
}
System.out.println();
}
static boolean isVowel( char x)
{
return (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U' );
}
static void removeVowels()
{
Node ptr = head;
while (ptr != null )
{
if (isVowel(ptr.data))
{
ptr = ptr.next;
}
else
{
break ;
}
}
Node prev = ptr;
head = ptr;
ptr = ptr.next;
while (ptr != null )
{
if (isVowel(ptr.data))
{
ptr = ptr.next;
while (ptr != null )
{
if (isVowel(ptr.data))
{
ptr = ptr.next;
}
else
{
break ;
}
}
if (ptr == null )
{
prev.next = null ;
break ;
}
else
{
prev.next = ptr;
}
}
prev = prev.next;
ptr = ptr.next;
}
}
public static void main (String[] args)
{
GFG tree = new GFG();
tree.head = new Node( 'a' );
tree.head.next = new Node( 'b' );
tree.head.next.next = new Node( 'c' );
tree.head.next.next.next = new Node( 'e' );
tree.head.next.next.next.next = new Node( 'f' );
tree.head.next.next.next.next.next = new Node( 'g' );
tree.head.next.next.next.next.next.next = new Node( 'i' );
tree.head.next.next.next.next.next.next.next = new Node( 'o' );
System.out.println( "Linked list before :" );
printlist(head);
removeVowels();
System.out.println( "Linked list after :" );
printlist(head);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self . next = None
head = None
def printlist(head):
if ( not head):
print ( "Empty List" )
return
while (head ! = None ):
print (head.data, end = " " )
if (head. next ):
print (end = "-> " )
head = head. next
print ()
def isVowel(x):
return (x = = 'a' or x = = 'e' or x = = 'i'
or x = = 'o' or x = = 'u' or x = = 'A'
or x = = 'E' or x = = 'I' or x = = 'O'
or x = = 'U' )
def removeVowels():
global head
ptr = head
while (ptr ! = None ):
if (isVowel(ptr.data)):
ptr = ptr. next
else :
break
prev = ptr
head = ptr
ptr = ptr. next
while (ptr ! = None ):
if (isVowel(ptr.data)):
ptr = ptr. next
while (ptr ! = None ):
if (isVowel(ptr.data)):
ptr = ptr. next
else :
break
if (ptr = = None ):
prev. next = None
break
else :
prev. next = ptr
prev = prev. next
ptr = ptr. next
if __name__ = = '__main__' :
head = Node( 'a' )
head. next = Node( 'b' )
head. next . next = Node( 'c' )
head. next . next . next = Node( 'e' )
head. next . next . next . next = Node( 'f' )
head. next . next . next . next . next = Node( 'g' )
head. next . next . next . next . next . next = Node( 'i' )
head. next . next . next . next . next . next . next = Node( 'o' )
print ( "Linked list before :" )
printlist(head)
removeVowels()
print ( "Linked list after :" )
printlist(head)
|
C#
using System;
class Node
{
public char data;
public Node next;
public Node( char item)
{
data = item;
next = null ;
}
}
class GFG{
static Node head;
static void printlist(Node head)
{
if (head == null )
{
Console.WriteLine( "Empty List" );
}
while (head != null )
{
Console.Write(head.data + " " );
if (head.next != null )
{
Console.Write( "-> " );
}
head = head.next;
}
Console.WriteLine();
}
static 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' );
}
static void removeVowels()
{
Node ptr = head;
while (ptr != null )
{
if (isVowel(ptr.data))
{
ptr = ptr.next;
}
else
{
break ;
}
}
Node prev = ptr;
head = ptr;
ptr = ptr.next;
while (ptr != null )
{
if (isVowel(ptr.data))
{
ptr = ptr.next;
while (ptr != null )
{
if (isVowel(ptr.data))
{
ptr = ptr.next;
}
else
{
break ;
}
}
if (ptr == null )
{
prev.next = null ;
break ;
}
else
{
prev.next = ptr;
}
}
prev = prev.next;
ptr = ptr.next;
}
}
static public void Main()
{
GFG.head = new Node( 'a' );
GFG.head.next = new Node( 'b' );
GFG.head.next.next = new Node( 'c' );
GFG.head.next.next.next = new Node( 'e' );
GFG.head.next.next.next.next = new Node( 'f' );
GFG.head.next.next.next.next.next = new Node( 'g' );
GFG.head.next.next.next.next.next.next = new Node( 'i' );
GFG.head.next.next.next.next.next.next.next = new Node( 'o' );
Console.WriteLine( "Linked list before :" );
printlist(head);
removeVowels();
Console.WriteLine( "Linked list after :" );
printlist(head);
}
}
|
Javascript
<script>
class Node {
constructor( item) {
this .data = item;
this .next = null ;
}
}
var head;
function printlist(head) {
if (head == null ) {
document.write( "Empty List" );
}
while (head != null ) {
document.write(head.data + " " );
if (head.next != null ) {
document.write( "-> " );
}
head = head.next;
}
document.write( "<br/>" );
}
function isVowel( x) {
return (x == 'a' || x == 'e' || x == 'i' || x == 'o'
|| x == 'u' || x == 'A' || x == 'E' || x == 'I'
|| x == 'O' || x == 'U' );
}
function removeVowels() {
var ptr = head;
while (ptr != null ) {
if (isVowel(ptr.data)) {
ptr = ptr.next;
}
else {
break ;
}
}
var prev = ptr;
head = ptr;
ptr = ptr.next;
while (ptr != null ) {
if (isVowel(ptr.data)) {
ptr = ptr.next;
while (ptr != null ) {
if (isVowel(ptr.data)) {
ptr = ptr.next;
}
else {
break ;
}
}
if (ptr == null ) {
prev.next = null ;
break ;
}
else {
prev.next = ptr;
}
}
prev = prev.next;
ptr = ptr.next;
}
}
head = new Node( 'a' );
head.next = new Node( 'b' );
head.next.next = new Node( 'c' );
head.next.next.next = new Node( 'e' );
head.next.next.next.next = new Node( 'f' );
head.next.next.next.next.next = new Node( 'g' );
head.next.next.next.next.next.next = new Node( 'i' );
head.next.next.next.next.next.next.next = new Node( 'o' );
document.write( "Linked list before :<br/>" );
printlist(head);
removeVowels();
document.write( "Linked list after :<br/>" );
printlist(head);
</script>
|
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.
Last Updated :
03 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...