HasCycle
Easy; Linkedlist;
Last updated
Easy; Linkedlist;
Last updated
输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。# 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 not head:
return False
fast, slow = head.next, head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if slow == fast:
return True
return False