text: Arrange consonants and vowels nodes in a linked list | A linked list node ; Utility function to print the linked list ; Utility function for checking vowel ; function to arrange consonants and vowels nodes ; for keep track of vowel ; list is empty ; We need to discover the first vowel in the list . It is going to be the returned head , and also the initial latestVowel . ; first element is a vowel . It will also be the new head and the initial latestVowel ; 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 . ; This is an edge case where there are only consonants in the list . ; 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 ; Now traverse the list . Curr is always the item * before * the one we are checking , so that we can use it to re - link . ; The next discovered item is a vowel ; If it comes directly after the previous vowel , we don 't need to  move items around, just mark the  new latestVowel and advance curr. ; 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 . ; Chain in new vowel ; Advance latestVowel ; Remove found vowel from previous place ; Re - link chain of consonants after latestVowel ; No vowel in the next element , advance curr . ; Driver code
code: class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def printlist ( head ) : NEW_LINE INDENT if ( not head ) : NEW_LINE INDENT print ( " Empty ▁ List " ) NEW_LINE return NEW_LINE DEDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE if ( head . next ) : NEW_LINE INDENT print ( end = " - > ▁ " ) NEW_LINE DEDENT head = head . next NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def isVowel ( x ) : NEW_LINE INDENT 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 ' ) NEW_LINE DEDENT def arrange ( head ) : NEW_LINE INDENT newHead = head NEW_LINE latestVowel = None NEW_LINE curr = head NEW_LINE if ( head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( isVowel ( head . data ) ) : NEW_LINE INDENT latestVowel = head NEW_LINE DEDENT else : NEW_LINE INDENT while ( curr . next != None and not isVowel ( curr . next . data ) ) : NEW_LINE INDENT curr = curr . next NEW_LINE DEDENT if ( curr . next == None ) : NEW_LINE INDENT return head NEW_LINE DEDENT latestVowel = newHead = curr . next NEW_LINE curr . next = curr . next . next NEW_LINE latestVowel . next = head NEW_LINE DEDENT while ( curr != None and curr . next != None ) : NEW_LINE INDENT if ( isVowel ( curr . next . data ) ) : NEW_LINE INDENT if ( curr == latestVowel ) : NEW_LINE INDENT latestVowel = curr = curr . next NEW_LINE DEDENT else : NEW_LINE INDENT temp = latestVowel . next NEW_LINE latestVowel . next = curr . next NEW_LINE latestVowel = latestVowel . next NEW_LINE curr . next = curr . next . next NEW_LINE latestVowel . next = temp NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT curr = curr . next NEW_LINE DEDENT DEDENT return newHead NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = Node ( ' a ' ) NEW_LINE head . next = Node ( ' b ' ) NEW_LINE head . next . next = Node ( ' c ' ) NEW_LINE head . next . next . next = Node ( ' e ' ) NEW_LINE head . next . next . next . next = Node ( ' d ' ) NEW_LINE head . next . next . next . next . next = Node ( ' o ' ) NEW_LINE head . next . next . next . next . next . next = Node ( ' x ' ) NEW_LINE head . next . next . next . next . next . next . next = Node ( ' i ' ) NEW_LINE print ( " Linked ▁ list ▁ before ▁ : " ) NEW_LINE printlist ( head ) NEW_LINE head = arrange ( head ) NEW_LINE print ( " Linked ▁ list ▁ after ▁ : " ) NEW_LINE printlist ( head ) NEW_LINE DEDENT