Description
Linked List Two Pointers
Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
# double pointers
f, l = head, head
# first encounter
while True:
if not f or not f.next: return None
f, l = f.next.next, l.next
if f == l: break
# second encounter
f = head
while f != l:
f, l = f.next, l.next
return f
END