腾讯24-环形链表leetcode141
给定一个链表,判断链表中是否有环。
leetcode或者剑指 offer最让人难忘的题目,快慢指针,比较好记住
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if head is None or head.next is None :
return False
else:
fast,slow=head,head
while(fast is not None and fast.next is not None ):
fast=fast.next.next
slow=slow.next
if fast==slow:
return True
return False