Given a linked list, determine if it has a cycle in it.
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if slow == fast:
return True
return False
1 一定要考虑特殊情况,比如这道题,如果input是[1]是没有环的
Given a linked list, determine if it has a cycle in it.
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if slow == fast:
return True
return False
1 一定要考虑特殊情况,比如这道题,如果input是[1]是没有环的