142. Linked List Cycle II
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
fast = head
slow = head
while(fast):
if fast.next is None:
return None
fast = fast.next.next
slow = slow.next
if fast == slow:
break
if fast is None:
return None
else:
slow = head
while(fast != slow):
fast = fast.next
slow = slow.next
return slowLast updated