Skip to content

Commit

Permalink
Add 141. Linked List Cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
HSILA committed Oct 16, 2024
1 parent 153f19b commit b4d51dc
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Linked List/141. Linked List Cycle/141_mine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://leetcode.com/problems/linked-list-cycle/
from typing import Optional


class ListNode:
def __init__(self, x):
self.val = x
self.next = None


class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow = head
fast = head

while head and fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
if fast == slow:
return True

return False

0 comments on commit b4d51dc

Please sign in to comment.