Skip to content

Commit 24767f3

Browse files
committed
solved linked-list-cycle
1 parent db93667 commit 24767f3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

linked-list-cycle/SamTheKorean.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Time complexity : O(n)
2+
# Space complexity : O(1)
3+
class Solution:
4+
def hasCycle(self, head: Optional[ListNode]) -> bool:
5+
slow = head
6+
fast = head
7+
8+
# check fast and fast.next are not the last node to ensure we can access fast.next.next
9+
while fast and fast.next:
10+
slow = slow.next
11+
fast = fast.next.next
12+
if slow == fast:
13+
return True
14+
15+
return False

0 commit comments

Comments
 (0)