Skip to content

Commit 1b97cec

Browse files
Merge pull request #517 from Iamtripathisatyam/main
Linked List Cycle 2
2 parents b0e4d13 + b034e11 commit 1b97cec

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Python/linked_list_cycle_2.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
3+
slow, fast = head, head
4+
while fast and fast.next:
5+
slow = slow.next
6+
fast = fast.next.next
7+
if slow == fast:
8+
break
9+
else:
10+
return None
11+
slow = head
12+
while slow != fast:
13+
slow = slow.next
14+
fast = fast.next
15+
return

0 commit comments

Comments
 (0)