1 classNode ():2 def __init__(self,item=None):3Self.item =Item4Self.next =None5 6 #def Doselinklistcontainsloop (head):7 #if head = = None:8 #print ("is an empty list")9 #return FalseTen #slowptr = Head One #fastptr = Head A #While Fastptr.next!=none and Fastptr.next.next!=none: - #slowptr = Slowptr.next - #fastptr = FastPtr.next.next the #if slowptr = = fastptr: - #print ("A linked list of ring structures") - #return True - #print ("Not a linked list of ring structures") + #return False - + defFindbeginofloop (head): ASlowptr =Head atFastptr =Head -Loopexist =False - ifHead = =None: - returnFalse - whileFastptr.next! = None andFastPtr.next.next! =None: -Slowptr =Slowptr.next inFastptr =FastPtr.next.next - ifSlowptr = =fastptr: toLoopexist =True + Print("existence Ring Structure") - Break the * ifLoopexist = =True: $Slowptr =HeadPanax Notoginseng whileSlowptr! =fastptr: -Fastptr =Fastptr.next theSlowptr =Slowptr.next + returnslowptr A the Print("not a ring structure .") + returnFalse - $ if __name__=="__main__": $Node1 = Node (1) -Node2 = Node (2) -Node3 = Node (3) theNode4 = Node (4) -NODE5 = Node (5)WuyiNode1.next =Node2 theNode2.next =Node3 -Node3.next =Node4 WuNode4.next =Node5 -Node5.next =Node2 About Print(Findbeginofloop (Node1). Item)
Determine if a one-way list includes loops, and if included, the node of the ring entry calculates the Python implementation