Related Articles
Swap the vowels in the linked list representation of a string
• Last Updated : 02 Dec, 2020

Given the linked list representation of a string, the task is to swap every subsequent pair of vowels in it to produce the resultant linked list.

Examples:

Input: List = g -> e -> e -> k -> s -> NULL
Output: g -> e -> e -> k -> s -> NULL
The only vowels are ‘e’ and ‘e’ which
will be swapped with each other.

Input: List = a -> b -> c -> d -> e -> NULL
Output: e -> b -> c -> d -> a -> NULL
swap(a, e)

Approach:

• Use two pointers of linked list node type for storing the node with the first vowel and for traversing the linked list respectively.
• Start traversing the linked list till the end.
• If a vowel is encountered and the first pointer already contains a node with a vowel in its data part, then swap the contents of the current node and the first node and the set the first pointer to NULL.
• In case a vowel is encountered and the first pointer does not contain anything, set the first pointer to point to this node.
• In case the vowel is not encountered, continue the traversal.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Structure to hold the data and``// the address of next element``struct` `node {``    ``char` `data;``    ``node* next;``};` `// Utility function to add a new``// node to the linked list``node* add(``char` `data)``{``    ``node* newnode = ``new` `node;``    ``newnode->data = data;``    ``newnode->next = NULL;``    ``return` `newnode;``}` `// Function to add the given character``// in the string to the linked list``node* addinll(node* head, ``char` `data)``{` `    ``// If the linked list is empty then add``    ``// it to the head otherwise add``    ``// the character to the end``    ``if` `(head == NULL)``        ``head = add(data);``    ``else` `{``        ``node* curr = head;``        ``while` `(curr->next != NULL)``            ``curr = curr->next;``        ``curr->next = add(data);``    ``}``    ``return` `head;``}` `// Function to print the linked list``void` `print(node* head)``{``    ``node* curr = head;``    ``while` `(curr != NULL) {``        ``cout << curr->data << ``" -> "``;``        ``curr = curr->next;``    ``}``    ``cout << ``"NULL"``;``}` `// Function that returns true if``// the character is vowel``bool` `isvowel(``char` `data)``{``    ``if` `(data == ``'a'` `|| data == ``'e'``        ``|| data == ``'i'` `|| data == ``'o'``        ``|| data == ``'u'``)``        ``return` `true``;``    ``if` `(data == ``'A'` `|| data == ``'E'``        ``|| data == ``'I'` `|| data == ``'O'``        ``|| data == ``'U'``)``        ``return` `true``;``    ``return` `false``;``}` `// Function to reverse the vowels in the linked list``void` `reversevowels(node* head)``{` `    ``// The pointer named first holds the``    ``// address of the node containing``    ``// the first vowel``    ``node* first = NULL;` `    ``// The pointer curr is used``    ``// to point to head``    ``node* curr = head;` `    ``// Traverse the linked list``    ``while` `(curr != NULL) {` `        ``// If a vowel is encountered and first is``        ``// set to NULL then update the first``        ``// with the address of this node``        ``if` `(isvowel(curr->data) and first == NULL)``            ``first = curr;` `        ``// If a vowel is encountered and the first``        ``// already contains the address of a vowel``        ``// then swap the current and the first's data``        ``else` `if` `(isvowel(curr->data) and first != NULL) {``            ``swap(first->data, curr->data);` `            ``// Set the first pointer to NULL``            ``first = NULL;``        ``}``        ``curr = curr->next;``    ``}``}` `// Driver code``int` `main()``{``    ``string s = ``"geeks"``;` `    ``// Start with an empty linked list``    ``node* head = NULL;` `    ``// Add the characters one by``    ``// one to the linked list``    ``for` `(``int` `i = 0; i < s.size(); i++)``        ``head = addinll(head, s[i]);` `    ``// Reverse the vowels``    ``// in the linked list``    ``reversevowels(head);` `    ``// Print the linked list``    ``// with reversed vowels``    ``print(head);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG{` `// Structure to hold the data and``// the address of next element``static` `class` `node``{``    ``char` `data;``    ``node next;``};` `// Utility function to add a new``// node to the linked list``static` `node add(``char` `data)``{``    ``node newnode = ``new` `node();``    ``newnode.data = data;``    ``newnode.next = ``null``;``    ``return` `newnode;``}` `// Function to add the given character``// in the String to the linked list``static` `node addinll(node head, ``char` `data)``{` `    ``// If the linked list is empty then add``    ``// it to the head otherwise add``    ``// the character to the end``    ``if` `(head == ``null``)``        ``head = add(data);``    ``else``    ``{``        ``node curr = head;``        ``while` `(curr.next != ``null``)``            ``curr = curr.next;``        ``curr.next = add(data);``    ``}``    ``return` `head;``}` `// Function to print the linked list``static` `void` `print(node head)``{``    ``node curr = head;``    ``while` `(curr != ``null``)``    ``{``        ``System.out.print(curr.data + ``" -> "``);``        ``curr = curr.next;``    ``}``    ``System.out.print(``"NULL"``);``}` `// Function that returns true if``// the character is vowel``static` `boolean` `isvowel(``char` `data)``{``    ``if` `(data == ``'a'` `|| data == ``'e'` `||``        ``data == ``'i'` `|| data == ``'o'` `||``        ``data == ``'u'``)``        ``return` `true``;``    ``if` `(data == ``'A'` `|| data == ``'E'` `||``        ``data == ``'I'` `|| data == ``'O'` `||``        ``data == ``'U'``)``        ``return` `true``;``    ``return` `false``;``}` `// Function to reverse the vowels in the linked list``static` `void` `reversevowels(node head)``{` `    ``// The pointer named first holds the``    ``// address of the node containing``    ``// the first vowel``    ``node first = ``null``;` `    ``// The pointer curr is used``    ``// to point to head``    ``node curr = head;` `    ``// Traverse the linked list``    ``while` `(curr != ``null``)``    ``{` `        ``// If a vowel is encountered and first is``        ``// set to null then update the first``        ``// with the address of this node``        ``if` `(isvowel(curr.data) && first == ``null``)``            ``first = curr;` `        ``// If a vowel is encountered and the first``        ``// already contains the address of a vowel``        ``// then swap the current and the first's data``        ``else` `if` `(isvowel(curr.data) && first != ``null``)``        ``{``            ``swap(first.data, curr.data);` `            ``// Set the first pointer to null``            ``first = ``null``;``        ``}``        ``curr = curr.next;``    ``}``}` `private` `static` `void` `swap(``char` `data, ``char` `data2)``{``    ``// Auto-generated method stub``    ``char` `newdata = data;``    ``data = data2;``    ``data2 = newdata;``    ` `}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``String s = ``"geeks"``;` `    ``// Start with an empty linked list``    ``node head = ``null``;` `    ``// Add the characters one by``    ``// one to the linked list``    ``for``(``int` `i = ``0``; i < s.length(); i++)``       ``head = addinll(head, s.charAt(i));` `    ``// Reverse the vowels``    ``// in the linked list``    ``reversevowels(head);` `    ``// Print the linked list``    ``// with reversed vowels``    ``print(head);``}``}` `// This code is contributed by gauravrajput1`

## Python3

 `# Python3 implementation of the approach` `# Structure to hold the data and``# the address of next element``class` `newNode:``    ` `    ``def` `__init__(``self``, data):``        ` `        ``self``.data ``=` `data``        ``self``.``next` `=` `None` `# Utility function to add a new``# node to the linked list``def` `add(data):``    ` `    ``newnode ``=` `newNode(data)``    ``return` `newnode` `# Function to add the given character``# in the string to the linked list``def` `addinll(head, data):``    ` `    ``# If the linked list is empty then add``    ``# it to the head otherwise add``    ``# the character to the end``    ``if` `(head ``=``=` `None``):``        ``head ``=` `add(data)``    ``else``:``        ``curr ``=` `head``        ` `        ``while` `(curr.``next` `!``=` `None``):``            ``curr ``=` `curr.``next``            ` `        ``curr.``next` `=` `add(data)``        ` `    ``return` `head` `# Function to print the linked list``def` `print1(head):``    ` `    ``curr ``=` `head``    ` `    ``while` `(curr !``=` `None``):``        ``print``(curr.data,end ``=` `" -> "``)``        ``curr ``=` `curr.``next``        ` `    ``print``(``"Null"``)` `# Function that returns true if``# the character is vowel``def` `isvowel(data):``    ` `    ``if` `(data ``=``=` `'a'` `or` `data ``=``=` `'e'` `or``        ``data ``=``=` `'i'` `or` `data ``=``=` `'o'` `or``        ``data ``=``=` `'u'``):``        ``return` `True``    ``if` `(data ``=``=` `'A'` `or` `data ``=``=` `'E'` `or``        ``data ``=``=` `'I'` `or` `data ``=``=` `'O'` `or``        ``data ``=``=` `'U'``):``        ``return` `True``        ` `    ``return` `False` `# Function to reverse the vowels in the``# linked list``def` `reversevowels(head):``    ` `    ``# The pointer named first holds the``    ``# address of the node containing``    ``# the first vowel``    ``first ``=` `None` `    ``# The pointer curr is used``    ``# to point to head``    ``curr ``=` `head` `    ``# Traverse the linked list``    ``while` `(curr !``=` `None``):` `        ``# If a vowel is encountered and first is``        ``# set to None then update the first``        ``# with the address of this node``        ``if` `(isvowel(curr.data) ``and` `first ``=``=` `None``):``            ``first ``=` `curr` `        ``# If a vowel is encountered and the first``        ``# already contains the address of a vowel``        ``# then swap the current and the first's data``        ``elif` `(isvowel(curr.data) ``and` `first !``=` `None``):``            ``temp ``=` `first.data``            ``first.data ``=` `curr.data``            ``curr.data ``=` `temp``            ` `            ``# Set the first pointer to None``            ``first ``=` `None``            ` `        ``curr ``=` `curr.``next` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``s ``=` `"geeks"` `    ``# Start with an empty linked list``    ``head ``=` `None` `    ``# Add the characters one by``    ``# one to the linked list``    ``for` `i ``in` `range``(``len``(s)):``        ``head ``=` `addinll(head, s[i])` `    ``# Reverse the vowels``    ``# in the linked list``    ``reversevowels(head)` `    ``# Print the linked list``    ``# with reversed vowels``    ``print1(head)` `# This code is contributed by SURENDRA_GANGWAR`

## C#

 `// C# implementation of the approach``using` `System;``class` `GFG{` `// Structure to hold the data and``// the address of next element``public` `class` `node``{``  ``public` `char` `data;``  ``public` `node next;``};` `// Utility function to add a new``// node to the linked list``static` `node add(``char` `data)``{``  ``node newnode = ``new` `node();``  ``newnode.data = data;``  ``newnode.next = ``null``;``  ``return` `newnode;``}` `// Function to add the given``// character in the String to``// the linked list``static` `node addinll(node head,``                    ``char` `data)``{``  ``// If the linked list is``  ``// empty then add it to the``  ``// head otherwise add the``  ``// character to the end``  ``if` `(head == ``null``)``    ``head = add(data);``  ``else``  ``{``    ``node curr = head;``    ``while` `(curr.next != ``null``)``      ``curr = curr.next;``    ``curr.next = add(data);``  ``}``  ``return` `head;``}` `// Function to print the linked list``static` `void` `print(node head)``{``  ``node curr = head;``  ``while` `(curr != ``null``)``  ``{``    ``Console.Write(curr.data + ``" -> "``);``    ``curr = curr.next;``  ``}``  ``Console.Write(``"NULL"``);``}` `// Function that returns true if``// the character is vowel``static` `bool` `isvowel(``char` `data)``{``  ``if` `(data == ``'a'` `|| data == ``'e'` `||``      ``data == ``'i'` `|| data == ``'o'` `||``      ``data == ``'u'``)``    ``return` `true``;``  ``if` `(data == ``'A'` `|| data == ``'E'` `||``      ``data == ``'I'` `|| data == ``'O'` `||``      ``data == ``'U'``)``    ``return` `true``;``  ``return` `false``;``}` `// Function to reverse the vowels``// in the linked list``static` `void` `reversevowels(node head)``{``  ``// The pointer named first``  ``// holds the address of the``  ``// node containing the first vowel``  ``node first = ``null``;` `  ``// The pointer curr is used``  ``// to point to head``  ``node curr = head;` `  ``// Traverse the linked list``  ``while` `(curr != ``null``)``  ``{``    ``// If a vowel is encountered and first is``    ``// set to null then update the first``    ``// with the address of this node``    ``if` `(isvowel(curr.data) &&``        ``first == ``null``)``      ``first = curr;` `    ``// If a vowel is encountered and``    ``// the first already contains the``    ``// address of a vowel then swap the``    ``// current and the first's data``    ``else` `if` `(isvowel(curr.data) &&``             ``first != ``null``)``    ``{``      ``swap(first.data, curr.data);` `      ``// Set the first pointer to null``      ``first = ``null``;``    ``}``    ``curr = curr.next;``  ``}``}` `private` `static` `void` `swap(``char` `data,``                         ``char` `data2)``{``  ``// Auto-generated method stub``  ``char` `newdata = data;``  ``data = data2;``  ``data2 = newdata;``    ` `}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``String s = ``"geeks"``;` `    ``// Start with an empty linked list``    ``node head = ``null``;` `    ``// Add the characters one by``    ``// one to the linked list``    ``for``(``int` `i = 0; i < s.Length; i++)``       ``head = addinll(head, s[i]);` `    ``// Reverse the vowels``    ``// in the linked list``    ``reversevowels(head);` `    ``// Print the linked list``    ``// with reversed vowels``    ``print(head);``}``}` `// This code contributed by gauravrajput1`
Output:
`g -> e -> e -> k -> s -> NULL`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up