# Python3 program to remove vowels
# Nodes in a linked list
 
# A linked list node
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
 
# Utility function to print the
# linked list
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()
 
# Utility function for checking vowel
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')
 
#/* function to arrange consonants and
#   vowels nodes */
def arrange(head):
    newHead = head
 
    # for keep track of vowel
    latestVowel = None
 
    curr = head
 
    # list is empty
    if (head == None):
        return None
 
    # We need to discover the first vowel
    # in the list. It is going to be the
    # returned head, and also the initial
    # latestVowel.
    if (isVowel(head.data)):
 
        # first element is a vowel. It will
        # also be the new head and the initial
        # latestVowel
        latestVowel = head
 
    else:
 
        # First element is not a vowel. Iterate
        # through the list until we find a vowel.
        # Note that curr points to the element
        # *before* the element with the vowel.
        while (curr.next != None and
               not isVowel(curr.next.data)):
            curr = curr.next
 
 
        # This is an edge case where there are
        # only consonants in the list.
        if (curr.next == None):
            return head
 
        # Set the initial latestVowel and the
        # new head to the vowel item that we found.
        # Relink the chain of consonants after
        # that vowel item:
        # old_head_consonant.consonant1.consonant2.
        # vowel.rest_of_list becomes
        # vowel.old_head_consonant.consonant1.
        # consonant2.rest_of_list
        latestVowel = newHead = curr.next
        curr.next = curr.next.next
        latestVowel.next = head
 
    # Now traverse the list. Curr is always the item
    # *before* the one we are checking, so that we
    # can use it to re-link.
    while (curr != None and curr.next != None):
        if (isVowel(curr.next.data)):
             
            # The next discovered item is a vowel
            if (curr == latestVowel):
                # If it comes directly after the
                # previous vowel, we don't need to
                # move items around, just mark the
                # new latestVowel and advance curr.
                latestVowel = curr = curr.next
            else:
 
                # But if it comes after an intervening
                # chain of consonants, we need to chain
                # the newly discovered vowel right after
                # the old vowel. Curr is not changed as
                # after the re-linking it will have a
                # new next, that has not been checked yet,
                # and we always keep curr at one before
                # the next to check.
                temp = latestVowel.next
 
                # Chain in new vowel
                latestVowel.next = curr.next
 
                # Advance latestVowel
                latestVowel = latestVowel.next
 
                # Remove found vowel from previous place
                curr.next = curr.next.next
 
                # Re-link chain of consonants after latestVowel
                latestVowel.next = temp
 
        else:
 
            # No vowel in the next element, advance curr.
            curr = curr.next
 
    return newHead
 
# Driver code
if __name__ == '__main__':
     
    # Initialise the Linked List
    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 the given Linked List
    print("Linked list before :")
    printlist(head)
 
    head = arrange(head)
 
    # Print the Linked List after
    # removing vowels
    print("Linked list after :")
    printlist(head)
 
# This code is contributed by mohit kumar 29

Time complexity: linear
Space complexity: constant