Skip to content

Commit

Permalink
feature: linked list cycle revisit
Browse files Browse the repository at this point in the history
  • Loading branch information
solairerove committed Jan 15, 2024
1 parent cb807b9 commit 1ef3fa1
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion linked_list/LinkedListCycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

# O(n) time || O(1) space
def has_cycle(self, head: Optional[ListNode]) -> bool:
slow = fast = head
if not head:
return False

slow, fast = head, head.next
while fast and fast.next:
slow, fast = slow.next, fast.next.next

if slow == fast:
return True

Expand Down

0 comments on commit 1ef3fa1

Please sign in to comment.