×

Arrange consonants and vowels nodes in a linked list

Arrange consonants and vowels nodes in a linked list

In this problem, we have given a singly linked list. Here we will arrange the consonants and vowels nodes of the list so that all the vowel nodes come before the consonants nodes. Also, we will maintain the order of their arrival.

Examples:

 Input:

a -> m -> k -> e -> d ->o -> z -> i

 Output:

a -> e -> o -> i -> m -> k -> d -> z

Method:

This method will take two array lists and traverse them to find the consonants and vowels. If we found a vowel while traversing, we will store it in the arraylist1; else, we will store it in arraylist2. After this, we will traverse the linked list again, take the data from the array lists, and put it into the linked list for arranging the vowel and consonants.

Source code to arrange consonants and vowels nodes in a linked list using Java programming:

 import java.util.*;
 public class Arrange
 {
     // Structure of the node
     static class Node
     {
         char data;
         Node next;
         public Node(char data)
         {
             this.data = data;
                 next = null;
         }
     }
 // For checking the character is vowel or not
     static boolean isVowel(char x)
     {
     return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u');
     }
 // For arrange consonants and vowels nodes in a linked list
     static void arrangeNodes(Node head)
     {
             // Arraylist for storing the vowel and consonants
             ArrayList<Character> vowel = new ArrayList<>();
             ArrayList<Character> consonants = new ArrayList<>();
             Node temp = head;
             while(temp != null)
             {
                         char t = temp.data;
                         // For  checking for vowel
                         if(isVowel(t))
                         {
                                     vowel.add(t);
                         }
                         else
                         {
                                     consonants.add(t);
                         }
                         temp = head;
                         int i = 0;
                         while(i < vowel.size())
                         {
                                     temp.data = vowel.get(i);
                                     i++;
                                     temp = temp.next;
                         }
                         i = 0;
                         while(i < consonants.size())
                         {
                                     temp.data = consonants.get(i);
                                     i++;
                                     temp = temp.next;
                         }             
           }
       }
     /* Method to print nodes in a given linked list */
     static void traverse(Node head)
     {
         while (head != null)
         {
             System.out.print(head.data + " ");
             head = head.next;
         }
     }
     // Driver method
     public static void main(String[] args)
     {
             Scanner sc = new Scanner(System.in);
             System.out.println("Enter the total no of elements");
             int t = sc.nextInt();
            char a1= sc.next().charAt(0);
             Node head= new Node(a1);
             Node tail = head;
             for (int i = 1; i < t; i++)
             {
                         char a = sc.next().charAt(0);
                         tail.next = (new Node(a));
                         tail = tail.next;
             }
         System.out.println("Linked list before rearranging the nodes :");
         traverse(head);
         arrangeNodes(head);
         System.out.println("\nLinked list after rearranging the nodes:");
         traverse(head);
     }
 } 

Output

Arrange consonants and vowels nodes in a linked list

Time Complexity: The time complexity of the above method is O(n).

Space Complexity: The space complexity of this method is O(n) because we use extra space for storing the vowel and consonants.


Related Topics

Hashing and its Applications

Hashing Hashing refers to transforming plain text data in such a way that even if it is leaked for some reason, no one would be able to make sense of it....

6 minutes read.

AVL Tree

AVL Tree AVL Tree is referred to as self-balanced or height-balanced binary search tree where the difference between heights of its left subtree and right subtree (Balance Factor) can't more than...

25 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Stack Using Linked List

In the linked list implementation of the stack, we use a linked list as the primitive data structure to create the stack. It is called the dynamic implementation of the...

6 minutes read.

What is a full Binary Tree?

A full binary tree is considered to be a special kind of binary tree in which every single node or leaf node present either contains two children or no children...

4 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Doubly Linked List

Doubly Linked List Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either...

4 minutes read.

Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false...

6 minutes read.

Binary Search Tree vs AVL Tree: Data Structure

Difference Between Binary Search Tree and AVL Tree Binary Search Tree: The binary search tree is a kind of binary tree data structure and it follows the conditions of binary...

3 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

Function to Create a Copy of Binary Search Tree

Implementation // creating a new hashmap in the language C++ that will help us clone a binary tree with arbitrary pointers.  #include<iostream> #include<unordered_map> using namespace std; /* A given binary tree has a record, a...

9 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

2 minutes read.

Binary Tree Uses

A binary tree is a tree data structure containing hubs with at most two children for instance a right and left child. The node at the top is insinuated as the...

3 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

What Is Graph Data Structure

A graph is generally a set of vertices and edges or border that is mainly used to join these vertices. A graph is basically pictured as a cyclic tree in...

7 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Sorting Algorithms in Data Structures

A sorting algorithm is used to organize the elements of an array or list. Sorting an array, for example. Unsorted array 572941 Sorted array 124579 We're sorting the array in ascending order right now. This procedure...

4 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 minutes read.