Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Define a function that takes the head of a linked list as an argument and returns true if the linked list has a loop, otherwise
Define a function that takes the head of a linked list as an argument and returns true if the linked list has a loop, otherwise it returns false. The definition of the linked list has already been given to you. class Node: def_init_(self, data): self.data = data self.next = None class LinkedList: def _init__(self): self. head = None def push(self, new_data): new_node = Node (new_data) new_node . next = self. head self. head = new_node def printlist(self): temp = self.head while (temp): print (temp.data) temp = temp next def detectLoop(self): \#write your code here 11ist = LinkedList () n = int(input()) data = input () if n==len(datasplit()) : for i in data,split(): list.push(int(i)) def push(self, new_data): new_node = Node ( new_data ) new_node. next = self. . head self. head = new_node def printlist(self): temp = self.head while (temp): print (temp.data) temp = temp.next def detectLoop(self): \#write your code here llist = LinkedList () n=int( input ()) data= input() if n==len( data.split ( )): for i in data.split(): llist.push (int (i)) key = int(input ( )) if key ==1 : llist. head. next. next =11 ist. head; if ( llist.detectLoop( )): print ("True") else : print ("False")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started