Arrange Consonants and Vowels | Practice | GeeksforGeeks

Given a singly linked list of size N containing only English Alphabets. Your task is to complete the function arrangeC&V(), that arranges the consonants and vowel nodes of the list it in such a way that all the vowels nodes come before the consonants while maintaining the order of their arrival.
Input:
The function takes a single argument as input, the reference pointer to the head of the linked list. There will be T test cases and for each test case the function will be called separately.
Output:
For each test case output a single line containing space separated elements of the list.
User Task:
The task is to complete the function arrange() which should arrange the vowels and consonants as required.
Constraints:
1 <= T <= 100
1 <= N <= 100
Example:
Input:
2
6
a e g h i m
3
q r t
Output:
a e i g h m
q r t
Explanation:
Testcase 1: Vowels like a, e and i are in the front, and consonants like g, h and m are at the end of the list.
 
0
harshilgupta00991 week ago
struct Node* arrange(Node *head)
{
   //Code here
   if(head==NULL || head->next==NULL)
    return head;
   Node* conshead=new Node('0');
   Node* vowelhead=new Node('0');
   Node* cons=conshead;
   Node* vowel=vowelhead;
   
   Node* cur=head;
   while(cur){
       if(cur->data=='a'||cur->data=='e'||cur->data=='i'||cur->data=='o'||cur->data=='u'){
           vowel->next=cur;
           vowel=vowel->next;
       }
       else{
           cons->next=cur;
           cons=cons->next;
       }
       cur=cur->next;
   }
   conshead=conshead->next;
   vowelhead=vowelhead->next;
   
   vowel->next=conshead;
   cons->next=NULL;
   
   if(vowelhead==NULL)
        return conshead;
   
   return vowelhead;
}
+1
sbbarse21212 weeks ago
// Append function Node* AppendLinkedList(Node* head,int data){ 
Node* new_node = new Node(data); 
if(head==NULL){ head = new_node; head->next = NULL;}else{ Node* temp = head; while(temp->next!=NULL){  temp = temp->next; } temp->next = new_node;
}return head;// cout<<"head val is"<<head->data<<endl;
}
 
// Main Answer
struct Node* arrange(Node *head){   vector<int>vowel;   vector<int>cons;   Node* temp = head;   while(temp){       if(temp->data=='a'||temp->data=='e'||temp->data=='i'||temp->data=='o'||temp->data=='u'){       vowel.push_back(temp->data);       }else{           cons.push_back(temp->data);       }              temp = temp->next;   }   Node* head1 = NULL;   for(auto itr:vowel){       head1 = AppendLinkedList(head1,itr);   }   for(auto itr1:cons){       head1 = AppendLinkedList(head1,itr1);   }   return head1;}
+1
shahabuddinbravo403 weeks ago
struct Node* arrange(Node *head){//   string c="",v="";   // T(n)=O(n) && space = O(n)//   Node *ptr=head;  //   while(ptr)//   {//       char ch=ptr->data;      //       if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){//           v.push_back(ch);//       }//       else{//           c.push_back(ch);//       }//       ptr=ptr->next;//   }  //   ptr=head;//   while(ptr){//       for(int i=0;i<v.size();i++){//           ptr->data=v[i];//           ptr=ptr->next;//       }//       for(int i=0;i<c.size();i++){//           ptr->data=c[i];//           ptr=ptr->next;//       }//   }//   return head;
   // T(n)=O(n) && space = O(1)     Node *vowel=new Node(-1),*consonent=new Node(-1),*v=vowel,*c=consonent,*ptr=head;            while(ptr)     {       char ch=ptr->data;       if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){            vowel->next=ptr;            vowel=vowel->next;       }       else{           consonent->next=ptr;           consonent=consonent->next;       }              ptr=ptr->next;              }          c=c->next;     v=v->next;          vowel->next=c;     consonent->next=NULL;          if(v==NULL){         return c;     }          head=v;     return head;}
0
rishu1 month ago
class Solution {
   
   public Node arrangeCV(Node head){
       //write code here and return the head of changed linked list
       Node vow = new Node('!');
       Node con = new Node('!');
       Node var1 = vow;
       Node var2 = con;
       
       Node temp = head;
       while(temp!=null){
           if(temp.data=='a'||temp.data=='e'||temp.data=='i'||temp.data=='o'||temp.data=='u' ){
               vow.next=temp;
               vow=vow.next;
           }else{
               con.next=temp;
               con=con.next;
           }
           temp=temp.next;
       }
       vow.next = var2.next;
       con.next=null;
       return var1.next;
   }
}
0
as0042301 month ago
In C++;
struct Node* arrange(Node *head){  Node *vow = NULL;  Node *vow_tail = NULL;  Node *cons = NULL;  Node *cons_tail = NULL;  Node *cur = head;    while(cur)  {      if(cur->data=='a' || cur->data=='e' || cur->data=='i' || cur->data=='o' || cur->data=='u')      {          if(vow == NULL){              vow = cur;              vow_tail = cur;          }          else{              vow_tail->next = cur;              vow_tail = vow_tail->next;          }      }      else      {           if(cons == NULL){              cons = cur;              cons_tail = cur;          }          else{              cons_tail->next = cur;              cons_tail = cons_tail->next;          }        }      cur = cur->next;  }    if(vow == NULL || cons == NULL)  return head;    vow_tail->next = cons;  cons_tail->next = NULL;  return vow;}
0
gupta2411sumit2 months ago
// { Driver Code Starts/*package whatever //do not write package name here */
import java.io.*;import java.util.*;
class Node {   char data;   Node next;      public Node(char data){       this.data = data;       next = null;   }}
class GFG {public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0){     int n = sc.nextInt();     Node head = null, tail = null;          char head_c = sc.next().charAt(0);     head = new Node(head_c);     tail = head;          while(n-- > 1){         tail.next = new Node(sc.next().charAt(0));         tail = tail.next;     }          Solution obj = new Solution();     //show(head);     show(obj.arrangeCV(head));      }}public static void po(Object o){    System.out.println(o);}public static void show(Node head){       while(head != null){           System.out.print(head.data+" ");           head = head.next;       }       System.out.println();   }}// } Driver Code Ends
/*Structure of node class is:class Node {   char data;   Node next;      public Node(char data){       this.data = data;       next = null;   }}*/class Solution {      public Node arrangeCV(Node head){         //write code here and return the head of changed linked list     Node v = new Node('a') ;    Node ans = v ;    Node c = new Node('b') ;    Node t = c ;    Node p = head ;        while (p != null)    {        if(p.data == 'a' || p.data == 'e' ||        p.data == 'i' || p.data == 'o' || p.data == 'u')        {            v.next = new Node(p.data) ;            v = v.next ;        }        else{            c.next = new Node(p.data) ;            c = c.next ;        }        p = p.next ;    }        v.next = t.next ;    head = ans.next ;    return head ;       }}
+1
badgujarsachin832 months ago
bool isvowel(int n){
    if(n=='a'||n=='e'||n=='i'||n=='o'||n=='u'){
        return true;
    }
    return false;
}
struct Node* arrange(Node *head)
{
   //Code here
   Node* curr=head;
   Node* newhead=new Node(0);
   Node* temp=newhead;
   while(curr){
       if(isvowel(curr->data)){
           temp->next=new Node(curr->data);
           temp=temp->next;
       }
       curr=curr->next;
   }
   curr=head;
   while(curr){
       if(!isvowel(curr->data)){
           temp->next=new Node(curr->data);
           temp=temp->next;
       }
       curr=curr->next;
   }
   return newhead->next;
}

0
varshithkotha0072 months ago
def arrangeCV(self, head):       # Code here       from collections import deque       dq=deque()       dq1=deque()       current=head       while current!=None:           if current.data=='a' or current.data=='e' or current.data==current.data=='i' or current.data=='o' or current.data=='u':               dq.append(current.data)           else:               dq1.append(current.data)           current=current.next       current1=head       while current1!=None and len(dq)!=0:           current1.data=dq.popleft()           current1=current1.next       while current1!=None and len(dq1)!=0:           current1.data=dq1.popleft()           current1=current1.next       return head
-1
rayalravi20012 months ago
c++ solution
 Node *curr = head;  Node *NewHead = new Node(0);  Node *neww = NewHead;  while(curr){      if(isVowel(curr->data)){          neww->next = new Node(curr->data);          neww = neww->next;      }      curr = curr->next;  }  curr = head;     while(curr){      if(!isVowel(curr->data)){          neww->next = new Node(curr->data);           neww = neww->next;      }      curr = curr->next;  }  return NewHead->next;
+2
kartikeyashokgautam3 months ago
Easy **JAVA** solution :-
 
  step 1. Create ArrayList arr , and store linkedlist data into it.
 step 2. Create Vowels and Consonants ArrayLists which will store Vowels and                                 consonants respectively.
step 3. Now first, add all elements of vowel ArrayList into given Linkedlist ( whose data              we stored into ArrayList arr ) and then add all elements of consonants ArrayList.
 
  public Node arrangeCV(Node head){                    ArrayList<Character> arr = new ArrayList<Character>();              Node temp = head;          // here we are storing data of linkedlist into arr           while(temp!=null)       {           arr.add(temp.data);           temp=temp.next;       }              ArrayList<Character> vowels = new ArrayList<Character>();// for storing only vowels        ArrayList<Character> consonants = new ArrayList<Character>();// for storing only                                                                                                                         consonants              for(int i=0;i<arr.size();i++)       {           if(arr.get(i)=='a' || arr.get(i)=='e' || arr.get(i)=='i' || arr.get(i)=='o' || arr.get(i)=='u' )           {               vowels.add(arr.get(i));           }                      else           {               consonants.add(arr.get(i));           }       }              // Here , adding elements of vowels and consonants arraylists into linked list respectively       // because return type of main function is  Node        temp=head;       int t1=0;       int t2=0;              while(temp!=null) // here we are first adding vowels and then consonants        {           if(t1<vowels.size())           {               temp.data=vowels.get(t1);               t1++;           }                      else           {               temp.data=consonants.get(t2);               t2++;           }          temp = temp.next;       }              return head;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.