Arrange consonants and vowels nodes in a linked list
Last Updated :
13 Jul, 2022
Given a singly linked list, we need to arrange the consonants and vowel nodes of it in such a way that all the vowel nodes come before the consonants while maintaining the order of their arrival.
Examples:
Input : a -> b -> c -> e -> d ->
o -> x -> i
Output : a -> e -> o -> i -> b ->
c -> d -> x
Solution :
The idea is to keep a marker of the latest vowel we found while traversing the list. If we find another vowel, we take it out of the chain and put it after the existing latest vowel. Example: For linked list:
a -> b -> c -> e -> d -> o -> x -> i
say our latestVowel reference references the ‘a’ node, and that we currently reached the ‘e’ node. We do:
a -> e -> b -> c -> d -> o -> x -> i
So what was after the ‘a’ node is now after the ‘e’ node after deleting it, and linking ‘a’ directly to ‘e’.
To properly remove and add links, it’s best to use the node before the one you are checking. So if you have a curr, you will check curr->next node to see if it’s a vowel or not. If it is, we need to add it after the latestVowel node, and then it’s easy to remove it from the chain by assigning its next to curr’s next. Also, if a list only contains consonants, we simply return head.
Implementation:
C
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct Node
{
char data;
struct Node *next;
};
struct Node *newNode( char key)
{
struct Node *temp = ( struct Node*) malloc ( sizeof ( struct Node));
temp->data = key;
temp->next = NULL;
return temp;
}
void printlist( struct Node *head)
{
if (! head)
{
printf ( "Empty list \n" );
return ;
}
while (head != NULL)
{
printf ( "%c" ,head->data);
if (head->next)
printf ( "->" );
head = head->next;
}
printf ( "\n" );
}
bool isVowel( char x)
{
return (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' );
}
struct Node *arrange( struct Node *head)
{
struct Node *newHead = head;
struct Node *latestVowel;
struct Node *curr = head;
if (head == NULL)
return NULL;
if (isVowel(head->data))
latestVowel = head;
else
{
while (curr->next != NULL &&
!isVowel(curr->next->data))
curr = curr->next;
if (curr->next == NULL)
return head;
latestVowel = newHead = curr->next;
curr->next = curr->next->next;
latestVowel->next = head;
}
while (curr != NULL && curr->next != NULL)
{
if (isVowel(curr->next->data))
{
if (curr == latestVowel)
{
latestVowel = curr = curr->next;
}
else
{
struct Node *temp = latestVowel->next;
latestVowel->next = curr->next;
latestVowel = latestVowel->next;
curr->next = curr->next->next;
latestVowel->next = temp;
}
}
else
{
curr = curr->next;
}
}
return newHead;
}
int main()
{
struct Node *head = newNode( 'a' );
head->next = newNode( 'b' );
head->next->next = newNode( 'c' );
head->next->next->next = newNode( 'e' );
head->next->next->next->next = newNode( 'd' );
head->next->next->next->next->next = newNode( 'o' );
head->next->next->next->next->next->next = newNode( 'x' );
head->next->next->next->next->next->next->next = newNode( 'i' );
printf ( "Linked list before :\n" );
printlist(head);
head = arrange(head);
printf ( "Linked list after :\n" );
printlist(head);
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
char data;
struct Node *next;
};
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' );
}
Node *arrange(Node *head)
{
Node *newHead = head;
Node *latestVowel;
Node *curr = head;
if (head == NULL)
return NULL;
if (isVowel(head->data))
latestVowel = head;
else
{
while (curr->next != NULL &&
!isVowel(curr->next->data))
curr = curr->next;
if (curr->next == NULL)
return head;
latestVowel = newHead = curr->next;
curr->next = curr->next->next;
latestVowel->next = head;
}
while (curr != NULL && curr->next != NULL)
{
if (isVowel(curr->next->data))
{
if (curr == latestVowel)
{
latestVowel = curr = curr->next;
}
else
{
Node *temp = latestVowel->next;
latestVowel->next = curr->next;
latestVowel = latestVowel->next;
curr->next = curr->next->next;
latestVowel->next = temp;
}
}
else
{
curr = curr->next;
}
}
return newHead;
}
int main()
{
Node *head = newNode( 'a' );
head->next = newNode( 'b' );
head->next->next = newNode( 'c' );
head->next->next->next = newNode( 'e' );
head->next->next->next->next = newNode( 'd' );
head->next->next->next->next->next = newNode( 'o' );
head->next->next->next->next->next->next = newNode( 'x' );
head->next->next->next->next->next->next->next = newNode( 'i' );
printf ( "Linked list before :\n" );
printlist(head);
head = arrange(head);
printf ( "Linked list after :\n" );
printlist(head);
return 0;
}
|
Java
class GfG
{
static class Node
{
char data;
Node next;
}
static Node newNode( char key)
{
Node temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
static void printlist(Node head)
{
if (head == null )
{
System.out.println( "Empty List" );
return ;
}
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' );
}
static Node arrange(Node head)
{
Node newHead = head;
Node latestVowel;
Node curr = head;
if (head == null )
return null ;
if (isVowel(head.data) == true )
latestVowel = head;
else
{
while (curr.next != null &&
!isVowel(curr.next.data))
curr = curr.next;
if (curr.next == null )
return head;
latestVowel = newHead = curr.next;
curr.next = curr.next.next;
latestVowel.next = head;
}
while (curr != null && curr.next != null )
{
if (isVowel(curr.next.data) == true )
{
if (curr == latestVowel)
{
latestVowel = curr = curr.next;
}
else
{
Node temp = latestVowel.next;
latestVowel.next = curr.next;
latestVowel = latestVowel.next;
curr.next = curr.next.next;
latestVowel.next = temp;
}
}
else
{
curr = curr.next;
}
}
return newHead;
}
public static void main(String[] args)
{
Node head = newNode( 'a' );
head.next = newNode( 'b' );
head.next.next = newNode( 'c' );
head.next.next.next = newNode( 'e' );
head.next.next.next.next = newNode( 'd' );
head.next.next.next.next.next = newNode( 'o' );
head.next.next.next.next.next.next = newNode( 'x' );
head.next.next.next.next.next.next.next = newNode( 'i' );
System.out.println( "Linked list before : " );
printlist(head);
head = arrange(head);
System.out.println( "Linked list after :" );
printlist(head);
}
}
|
C#
using System;
class GfG
{
public class Node
{
public char data;
public Node next;
}
static Node newNode( char key)
{
Node temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
static void printlist(Node head)
{
if (head == null )
{
Console.WriteLine( "Empty List" );
return ;
}
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' );
}
static Node arrange(Node head)
{
Node newHead = head;
Node latestVowel;
Node curr = head;
if (head == null )
return null ;
if (isVowel(head.data) == true )
latestVowel = head;
else
{
while (curr.next != null &&
!isVowel(curr.next.data))
curr = curr.next;
if (curr.next == null )
return head;
latestVowel = newHead = curr.next;
curr.next = curr.next.next;
latestVowel.next = head;
}
while (curr != null && curr.next != null )
{
if (isVowel(curr.next.data) == true )
{
if (curr == latestVowel)
{
latestVowel = curr = curr.next;
}
else
{
Node temp = latestVowel.next;
latestVowel.next = curr.next;
latestVowel = latestVowel.next;
curr.next = curr.next.next;
latestVowel.next = temp;
}
}
else
{
curr = curr.next;
}
}
return newHead;
}
public static void Main(String[] args)
{
Node head = newNode( 'a' );
head.next = newNode( 'b' );
head.next.next = newNode( 'c' );
head.next.next.next = newNode( 'e' );
head.next.next.next.next = newNode( 'd' );
head.next.next.next.next.next = newNode( 'o' );
head.next.next.next.next.next.next = newNode( 'x' );
head.next.next.next.next.next.next.next = newNode( 'i' );
Console.WriteLine( "Linked list before : " );
printlist(head);
head = arrange(head);
Console.WriteLine( "Linked list after :" );
printlist(head);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self . next = 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 arrange(head):
newHead = head
latestVowel = None
curr = head
if (head = = None ):
return None
if (isVowel(head.data)):
latestVowel = head
else :
while (curr. next ! = None and
not isVowel(curr. next .data)):
curr = curr. next
if (curr. next = = None ):
return head
latestVowel = newHead = curr. next
curr. next = curr. next . next
latestVowel. next = head
while (curr ! = None and curr. next ! = None ):
if (isVowel(curr. next .data)):
if (curr = = latestVowel):
latestVowel = curr = curr. next
else :
temp = latestVowel. next
latestVowel. next = curr. next
latestVowel = latestVowel. next
curr. next = curr. next . next
latestVowel. next = temp
else :
curr = curr. next
return newHead
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( 'd' )
head. next . next . next . next . next = Node( 'o' )
head. next . next . next . next . next . next = Node( 'x' )
head. next . next . next . next . next . next . next = Node( 'i' )
print ( "Linked list before :" )
printlist(head)
head = arrange(head)
print ( "Linked list after :" )
printlist(head)
|
Javascript
<script>
class Node
{
constructor(key)
{
this .data=key;
this .next = null ;
}
}
function printlist(head)
{
if (head == null )
{
document.write( "Empty List<br>" );
return ;
}
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' );
}
function arrange(head)
{
let newHead = head;
let latestVowel;
let curr = head;
if (head == null )
return null ;
if (isVowel(head.data) == true )
latestVowel = head;
else
{
while (curr.next != null &&
!isVowel(curr.next.data))
curr = curr.next;
if (curr.next == null )
return head;
latestVowel = newHead = curr.next;
curr.next = curr.next.next;
latestVowel.next = head;
}
while (curr != null && curr.next != null )
{
if (isVowel(curr.next.data) == true )
{
if (curr == latestVowel)
{
latestVowel = curr = curr.next;
}
else
{
let temp = latestVowel.next;
latestVowel.next = curr.next;
latestVowel = latestVowel.next;
curr.next = curr.next.next;
latestVowel.next = temp;
}
}
else
{
curr = curr.next;
}
}
return newHead;
}
let 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(' d ');
head.next.next.next.next.next = new Node(' o ');
head.next.next.next.next.next.next = new Node(' x ');
head.next.next.next.next.next.next.next = new Node(' i');
document.write( "Linked list before : <br>" );
printlist(head);
head = arrange(head);
document.write( "Linked list after :<br>" );
printlist(head);
</script>
|
Output
Linked list before :
a->b->c->e->d->o->x->i
Linked list after :
a->e->o->i->b->c->d->x
TIME COMPLEXITY:- O(n)
SPACE COMPLEXITY:- O(1)
Another approach:-
Another approach to solve the above problem is to create two separate Linked List one containing only vowels and the other one containing only consonant.
Remember the below approach is simple to understand BUT IT REQUIRES A SPACE COMPLEXITY OF O(N)
While traversing the given input Linked List if the node data is vowel then add it to the vowel Linked List and if the node data is consonant then add it to the consonant Linked List.
After traversal you have to just link the last vowel node to the first consonant node of the respective Linked List.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
struct Node* next;
Node( int x)
{
data = x;
next = NULL;
}
};
void append( struct Node** headRef, char data)
{
struct Node* new_node = new Node(data);
struct Node* last = *headRef;
if (*headRef == NULL) {
*headRef = new_node;
return ;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return ;
}
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;
}
struct Node* arrange(Node* head)
{
Node *vowel = NULL, *consonant = NULL, *start = NULL,
*end = NULL;
while (head != NULL) {
char x = head->data;
if (x == 'a' || x == 'e' || x == 'i' || x == 'o'
|| x == 'u' ) {
if (!vowel) {
vowel = new Node(x);
start = vowel;
}
else {
vowel->next = new Node(x);
vowel = vowel->next;
}
}
else {
if (!consonant) {
consonant = new Node(x);
end = consonant;
}
else {
consonant->next = new Node(x);
consonant = consonant->next;
}
}
head = head->next;
}
if (start == NULL)
return end;
vowel->next = end;
return start;
}
int main()
{
struct Node* head = NULL;
append(&head, 'a' );
append(&head, 'b' );
append(&head, 'c' );
append(&head, 'e' );
append(&head, 'd' );
append(&head, 'o' );
append(&head, 'x' );
append(&head, 'i' );
printf ( "Linked list before :\n" );
printlist(head);
head = arrange(head);
printf ( "Linked list after :\n" );
printlist(head);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
char data;
Node next;
Node( char x)
{
this .data = x;
this .next = null ;
}
};
static Node append(Node headRef, char data)
{
Node new_node = new Node(data);
Node last = headRef;
if (headRef == null ) {
headRef = new_node;
return headRef;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return headRef;
}
static void printlist(Node head)
{
if (head == null ) {
System.out.print( "Empty List\n" );
return ;
}
while (head != null ) {
System.out.print(head.data+ " " );
if (head.next!= null )
System.out.print( "-> " );
head = head.next;
}
System.out.println();
}
@SuppressWarnings ( "null" )
static Node arrange(Node head)
{
Node vowel = null , consonant = null , start = null ,
end = null ;
while (head != null ) {
char x = head.data;
if (x == 'a' || x == 'e' || x == 'i' || x == 'o'
|| x == 'u' ) {
if (vowel== null ) {
vowel = new Node(x);
start = vowel;
}
else {
vowel.next = new Node(x);
vowel = vowel.next;
}
}
else {
if (consonant == null ) {
consonant = new Node(x);
end = consonant;
}
else {
consonant.next = new Node(x);
consonant = consonant.next;
}
}
head = head.next;
}
if (start == null )
return end;
vowel.next = end;
return start;
}
public static void main(String[] args)
{
Node head = null ;
head = append(head, 'a' );
head = append(head, 'b' );
head = append(head, 'c' );
head = append(head, 'e' );
head = append(head, 'd' );
head = append(head, 'o' );
head = append(head, 'x' );
head = append(head, 'i' );
System.out.printf( "Linked list before :\n" );
printlist(head);
head = arrange(head);
System.out.printf( "Linked list after :\n" );
printlist(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data;
self . next = None ;
def append(headRef, data):
new_Node = Node(data);
last = headRef;
if (headRef = = None ):
headRef = new_Node;
return headRef;
while (last. next ! = None ):
last = last. next ;
last. next = new_Node;
return headRef;
def printlist(head):
if (head = = None ):
print ( "Empty List" );
return ;
while (head ! = None ):
print (head.data, end = "");
if (head. next ! = None ):
print ( "-> " ,end = "");
head = head. next ;
print ();
def arrange(head):
vowel = None ;
consonant = None ;
start = None ;
end = None ;
while (head ! = None ):
x = head.data;
if (x = = 'a' or x = = 'e' or x = = 'i' or x = = 'o' or x = = 'u' ):
if (vowel = = None ):
vowel = Node(x);
start = vowel;
else :
vowel. next = Node(x);
vowel = vowel. next ;
else :
if (consonant = = None ):
consonant = Node(x);
end = consonant;
else :
consonant. next = Node(x);
consonant = consonant. next ;
head = head. next ;
if (start = = None ):
return end;
vowel. next = end;
return start;
if __name__ = = '__main__' :
head = None ;
head = append(head, 'a' );
head = append(head, 'b' );
head = append(head, 'c' );
head = append(head, 'e' );
head = append(head, 'd' );
head = append(head, 'o' );
head = append(head, 'x' );
head = append(head, 'i' );
print ( "Linked list before :" );
printlist(head);
head = arrange(head);
print ( "Linked list after :" );
printlist(head);
|
C#
using System;
public class GFG {
public class Node {
public char data;
public Node next;
public Node( char x) {
this .data = x;
this .next = null ;
}
};
static Node append(Node headRef, char data) {
Node new_node = new Node(data);
Node last = headRef;
if (headRef == null ) {
headRef = new_node;
return headRef;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return headRef;
}
static void printlist(Node head) {
if (head == null ) {
Console.Write( "Empty List\n" );
return ;
}
while (head != null ) {
Console.Write(head.data + " " );
if (head.next != null )
Console.Write( "-> " );
head = head.next;
}
Console.WriteLine();
}
static Node arrange(Node head) {
Node vowel = null , consonant = null , start = null , end = null ;
while (head != null ) {
char x = head.data;
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' ) {
if (vowel == null ) {
vowel = new Node(x);
start = vowel;
} else {
vowel.next = new Node(x);
vowel = vowel.next;
}
} else {
if (consonant == null ) {
consonant = new Node(x);
end = consonant;
} else {
consonant.next = new Node(x);
consonant = consonant.next;
}
}
head = head.next;
}
if (start == null )
return end;
vowel.next = end;
return start;
}
public static void Main(String[] args) {
Node head = null ;
head = append(head, 'a' );
head = append(head, 'b' );
head = append(head, 'c' );
head = append(head, 'e' );
head = append(head, 'd' );
head = append(head, 'o' );
head = append(head, 'x' );
head = append(head, 'i' );
Console.Write( "Linked list before :\n" );
printlist(head);
head = arrange(head);
Console.Write( "Linked list after :\n" );
printlist(head);
}
}
|
Javascript
<script>
class Node {
constructor(x) {
this .data = x;
this .next = null ;
}
}
function append(headRef, data) {
var new_node = new Node(data);
var last = headRef;
if (headRef == null ) {
headRef = new_node;
return headRef;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return headRef;
}
function printlist(head) {
if (head == null ) {
document.write( "Empty List\n" );
return ;
}
while (head != null ) {
document.write(head.data + " " );
if (head.next != null )
document.write( "-> " );
head = head.next;
}
document.write();
}
function arrange(head) {
var vowel = null , consonant = null , start = null , end = null ;
while (head != null ) {
var x = head.data;
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' ) {
if (vowel == null ) {
vowel = new Node(x);
start = vowel;
} else {
vowel.next = new Node(x);
vowel = vowel.next;
}
} else {
if (consonant == null ) {
consonant = new Node(x);
end = consonant;
} else {
consonant.next = new Node(x);
consonant = consonant.next;
}
}
head = head.next;
}
if (start == null )
return end;
vowel.next = end;
return start;
}
var head = null ;
head = append(head, 'a' );
head = append(head, 'b' );
head = append(head, 'c' );
head = append(head, 'e' );
head = append(head, 'd' );
head = append(head, 'o' );
head = append(head, 'x' );
head = append(head, 'i' );
document.write( "Linked list before :<br/>" );
printlist(head);
head = arrange(head);
document.write( "<br/>Linked list after :<br/>" );
printlist(head);
</script>
|
Output
Linked list before :
a -> b -> c -> e -> d -> o -> x -> i
Linked list after :
a -> e -> o -> i -> b -> c -> d -> x
TIME COMPLEXITY:- O(n)
SPACE COMPLEXITY:- O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...