Move vowels node at the beginning of linked list in python
Python program for Move vowels node at the beginning of linked list. Here problem description and other solutions.
# Python 3 program for
# Shift the vowels node at beginning of linked list
# Linked list node
class LinkNode :
def __init__(self, data) :
self.data = data
self.next = None
class SingleLL :
def __init__(self) :
self.head = None
self.tail = None
# Add new node at end of linked list
def addNode(self, data) :
node = LinkNode(data)
if (self.head == None) :
self.head = node
else :
# Append the node at last position
self.tail.next = node
self.tail = node
# Display linked list element
def display(self) :
if (self.head == None) :
print("\n Empty linked list")
return
temp = self.head
# iterating linked list elements
while (temp != None) :
print("", temp.data ,end = " →")
# Visit to next node
temp = temp.next
print(" NULL")
# Determine that given value is an vowel or not
def isVowels(self, value) :
if (value == 'a'
or value == 'e'
or value == 'i'
or value == 'o'
or value == 'u'
or value == 'A'
or value == 'E'
or value == 'I'
or value == 'O'
or value == 'U') :
return True
return False
# Move the vowels value at beginning position
def shiftVowels(self) :
if (self.head == None) :
print("\n Empty linked list")
return
# Define some auxiliary variable
node = self.head
back = None
# iterate linked list nodes
# And shift vowels at front of linked list
while (node != None) :
if (self.isVowels(node.data) == True and back != None) :
# Enter here when
# Node is contain vowel and
# its previous (any) node is not form of vowel
if (node == self.tail) :
# When is last node
# Set new last node
self.tail = back
# Connect previous node to next node
back.next = node.next
# Connect vowel node to first node
node.next = self.head
# Set new head
self.head = node
# Get back node
node = back
else :
back = node
# Visit to next node
node = node.next
def main() :
sll = SingleLL()
# Constructed linked list
sll.addNode('E')
sll.addNode('D')
sll.addNode('U')
sll.addNode('C')
sll.addNode('A')
sll.addNode('T')
sll.addNode('I')
sll.addNode('O')
sll.addNode('N')
print(" Before move vowels")
# E → D → U → C → A → T → I → O → N → NULL
sll.display()
sll.shiftVowels()
print(" After move vowels")
# O → I → A → U → E → D → C → T → N → NULL
sll.display()
if __name__ == "__main__": main()
Output
Before move vowels
E → D → U → C → A → T → I → O → N → NULL
After move vowels
O → I → A → U → E → D → C → T → N → NULL
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment