Linked List Cycle II LeetCode solution in Python

1
# Definition for singly-linked list.
2
# class ListNode:
3
# def __init__(self, x):
4
# self.val = x
5
# self.next = None
6

7

8
class Solution:
9
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
10
if not head or not head.next:
11
return None
12
slow = fast = entry = head
13

14
while fast and fast.next:
15
slow = slow.next
16
fast = fast.next.next
17

18
if slow == fast:
19
while slow != entry:
20
slow = slow.next
21
entry = entry.next
22
return entry
23
return None

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0