Skip to main content

Reverse the linked list by words in python

Python program for Reverse the linked list by words. Here problem description and other solutions.

#  Python 3 program for
#  Reverse each word in a linked list node

#  Linked list node
class LinkNode :
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	def __init__(self) :
		self.head = None
	
	def addNode(self, data) :
		node = LinkNode(data)
		node.next = self.head
		#  New first node
		self.head = node
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			return
		
		temp = self.head
		#  iterating linked list elements
		while (temp != None) :
			if (temp.data == ' ') :
				print(end = " ' '")
			else :
				print("", temp.data, end = "")
			
			print(end = " →")
			#  Visit to next node
			temp = temp.next
		
		print(" NULL")
	
	#  Reverse a single linked list
	def reverse(self) :
		if (self.head == None) :
			print("Empty linked list")
		else :
			back = None
			temp = self.head
			while (temp != None) :
				#  New head node
				self.head = temp
				#  Visit to next node
				temp = temp.next
				#  Connect with previous node
				self.head.next = back
				#  Get current node
				back = self.head
			
		
	
	#  Reversing words in linked list
	def reverseWord(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		#  Define some auxiliary variables
		temp = self.head
		start = None
		auxiliary = None
		separator = None
		hold = None
		self.head = None
		#  iterating linked list elements
		while (temp != None) :
			if (temp.data == ' ') :
				hold = temp
				#  Visit to next node
				temp = temp.next
				#  collect space ' '
				hold.next = separator
				separator = hold
				if (auxiliary != None) :
					auxiliary.next = None
				
				if (self.head == None) :
					self.head = start
				elif (start != None and auxiliary != None) :
					#  When more than one word exists
					hold = separator
					separator = hold.next
					auxiliary.next = hold
					hold.next = self.head
					self.head = start
				
				start = None
				auxiliary = None
			else :
				if (start == None) :
					start = temp
				
				auxiliary = temp
				#  Visit to next node
				temp = temp.next
			
		
		#  When no word exists
		#  Or Manage last word
		if (separator != None) :
			hold = separator
			#  When more than two space exist
			while (hold.next != None) :
				hold = hold.next
			
			hold.next = self.head
			self.head = separator
		
		if (auxiliary != None and start != None) :
			auxiliary.next = self.head
			self.head = start
		
		self.reverse()
	

def main() :
	sll = SingleLL()
	#   Constructed linked list
	sll.addNode('e')
	sll.addNode('d')
	sll.addNode('o')
	sll.addNode('C')
	sll.addNode(' ')
	sll.addNode('d')
	sll.addNode('o')
	sll.addNode('o')
	sll.addNode('G')
	sll.addNode(' ')
	sll.addNode('s')
	sll.addNode('I')
	sll.addNode(' ')
	sll.addNode('s')
	sll.addNode('i')
	sll.addNode('h')
	sll.addNode('T')
	#  T → h → i → s → ' ' → I → s → ' ' → 
	#  G → o → o → d → ' ' → C → o → d → e → NULL
	sll.display()
	sll.reverseWord()
	#  s → i → h → T → ' ' → s → I → ' ' → d → o → 
	#  o → G → ' ' → e → d → o → C → NULL
	sll.display()

if __name__ == "__main__": main()

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 s → i → h → T → ' ' → s → I → ' ' → d → o → o → G → ' ' → e → d → o → C → NULL




Comment

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