如何通过Python代码检测链表是否存在环?

2026-06-09 23:016阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计505个文字,预计阅读时间需要3分钟。

如何通过Python代码检测链表是否存在环?

首先,我们来看一下原始代码:

pythonclass 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

如何通过Python代码检测链表是否存在环?

接下来,我们将这段代码简化,不超过100个字:

pythonclass Node: def __init__(self, value=None): self.value, self.next=value, None

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

def get_head_node(self): return self.head

先看下实例代码:

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()

知识点思考:

判断一个单链表是否有环,

可以用 set 存放每一个 节点, 这样每次 访问后把节点丢到这个集合里面.

其实 可以遍历这个单链表, 访问过后,

如果这个节点 不在 set 里面, 把这个节点放入到 set 集合里面.

如果这个节点在 set 里面 , 说明曾经访问过, 所以这个链表有重新 走到了这个节点, 因此一定有环

如果链表都走完了, 把所有的节点都放完了. 还是没有重复的节点, 那说明没有环.

以上就是本次介绍的全部相关知识点内容,感谢大家的学习和对易盾网络的支持。

本文共计505个文字,预计阅读时间需要3分钟。

如何通过Python代码检测链表是否存在环?

首先,我们来看一下原始代码:

pythonclass 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

如何通过Python代码检测链表是否存在环?

接下来,我们将这段代码简化,不超过100个字:

pythonclass Node: def __init__(self, value=None): self.value, self.next=value, None

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

def get_head_node(self): return self.head

先看下实例代码:

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()

知识点思考:

判断一个单链表是否有环,

可以用 set 存放每一个 节点, 这样每次 访问后把节点丢到这个集合里面.

其实 可以遍历这个单链表, 访问过后,

如果这个节点 不在 set 里面, 把这个节点放入到 set 集合里面.

如果这个节点在 set 里面 , 说明曾经访问过, 所以这个链表有重新 走到了这个节点, 因此一定有环

如果链表都走完了, 把所有的节点都放完了. 还是没有重复的节点, 那说明没有环.

以上就是本次介绍的全部相关知识点内容,感谢大家的学习和对易盾网络的支持。