Skip to content

Commit 2d81332

Browse files
committed
LeetCode #141: Linked List Cycle
1 parent 5ca1123 commit 2d81332

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

LeetCode/easy/has_cycle_141.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Optional
2+
3+
4+
class ListNode:
5+
def __init__(self, x):
6+
self.val = x
7+
self.next = None
8+
9+
10+
class Solution:
11+
def hasCycle(self, head: Optional[ListNode]) -> bool:
12+
slow, fast = head, head
13+
14+
while fast and fast.next:
15+
slow = slow.next
16+
fast = fast.next.next
17+
18+
if fast == slow:
19+
return True
20+
21+
return False

0 commit comments

Comments
 (0)