版权声明:转载请注明出处 https://blog.csdn.net/JNingWei/article/details/83858024
Difficulty
Easy.
Problem
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
AC
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution():
def hasCycle(self, head):
if not head or not head.next:
return False
slow = fast = head
while fast.next and fast.next.next:
slow, fast = slow.next, fast.next.next
if fast == slow:
return True
return False