1,快慢指针解决 判断链表是否有环应该是老生常谈的一个话题了,最简单的一种方式就是快慢指针,慢指针针每次走一步,快指针每次走两步,如果相遇就说明有环,如果有一个为空说明没有环
#链表节点 class ListNode: def __init__(self,node_value): self.value =node_value self.next =None #默认情况,下一个元素None # #判断 class Solution: def hasCycle(self , head ): # write code here if head is None or head.next is None: return False first = head.next last = head.next.next while last is not None: if first.val == last.val: return True first = first.next if last.next is None or last.next.next is None: return False last = last.next.next return False