Skip to main content

Check linked list is circular or not in python

Python program for Check linked list is circular or not . Here problem description and explanation.

#  Python 3 Program for
#  Check linked list is circular or not

#  Define class of linked list Node
class LinkNode :
	def __init__(self, data) : = data = None

class LinkedList :
	#  Class constructor
	def __init__(self) :
		self.head = None
	#  Check circular linked list or not
	#  Note that this function is not capable to detect loop
	def isCircular(self) :
		if (self.head == None) :
			#  Case when linked list is empty
			return False
		else :
			temp = self.head
			while (temp != None) :
				#  Visit to next node
				temp =
				if (temp == self.head) :
					#  When detecting circular node
					return True
			#  When not circular linked list
			return False

def main() :
	ll = LinkedList()
	#  insert element of linked list
	ll.head = LinkNode(1) = LinkNode(2) = LinkNode(3) = LinkNode(4) = LinkNode(5)
	if (ll.isCircular()) :
	else :
	#  Connect last node to head = ll.head
	if (ll.isCircular()) :
	else :

if __name__ == "__main__": main()




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