Skip to main content

Linked List Cycle II


Given a linked list, determine if it contains a cycle and if so, find the node at which the cycle begins.

Example 1
Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1


all//Linked List Cycle

# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x = None

class Solution:
def detectCycle(self, head):
slow = fast = head
while fast and
slow =
fast =
if slow == fast:
return None

slow = head
while slow != fast:
slow =
fast =
return slow