首页 > 编程语言 > 详细

python判断链表是否有环

时间:2020-01-30 23:19:15      阅读:142      评论:0      收藏:0      [点我收藏+]
class Node:
    def __init__(self,value=None):
        self.value = value
        self.next = None

class LinkList:
    def __init__(self,head = None):
        self.head = head

    def get_head_node(self):
        """
        获取头部节点
        """
        return self.head
        
    def append(self,value) :
        """
        从尾部添加元素
        """   
        node = Node(value = value)  
        cursor = self.head 
        if self.head is None:
            self.head = node
        else:    
            while cursor.next is not None:
                cursor = cursor.next
        
 
            cursor.next = node
            if value==4:
                node.next = self.head
    
    def traverse_list(self):
        head = self.get_head_node()
        cursor = head
        while cursor is not None:
            print(cursor.value)
            cursor = cursor.next
        print("traverse_over") 
        
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
       """
        slow=fast=head
        while slow and fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow is fast:
                return True
        return False
    

    
def main():
    l = LinkList()
    l.append(1)
    l.append(2)
    l.append(3)
    l.append(4)
    head = l.get_head_node()
    print(l.hasCycle(head))
    #l.traverse_list()


if __name__ == "__main__":
    main()
    

python判断链表是否有环

原文:https://www.cnblogs.com/c-x-a/p/12244236.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!