type
status
date
slug
summary
tags
category
icon
password
URL
Detecting the Start of a Cycle in a Linked List
Problem Statement:
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return
null
. A cycle exists in a linked list if there is a node that can be reached again by continuously following the next
pointer. You are not allowed to modify the linked list.Understanding the Solution with Two Pointers
To detect a cycle in a linked list and find the start of that cycle, we use two pointers: slow and fast. Let's dive into the approach step-by-step.
Step 1: Initialize Slow and Fast Pointers
- We start with two pointers:
slow
andfast
, both initially pointing to the head of the linked list.
- The slow pointer moves one step at a time (
slow = slow->next
).
- The fast pointer moves two steps at a time (
fast = fast->next->next
).
Step 2: Detecting the Cycle
- We move both pointers in their respective steps until they either meet or reach the end of the list.
- If there is a cycle, the fast pointer will eventually "catch up" to the slow pointer inside the cycle. This is similar to a faster runner on a circular track eventually catching up with a slower runner.
- If there is no cycle, the fast pointer will reach the end (
null
), and we can returnnull
indicating no cycle is present.
Step 3: Finding the Entry Point of the Cycle
- Once we detect the cycle (i.e.,
slow
andfast
meet), we need to determine the start of the cycle.
- To find the entry point, we initialize another pointer,
entry
, at the head of the linked list.
- We then move both the
entry
andslow
pointers one step at a time until they meet. The point at which they meet is the start of the cycle.
Why Does This Work?
Let's break down why this approach works:
- Meeting Point Logic:
- Suppose the linked list is divided into two parts:
- The distance from the head to the start of the cycle is
a
. - The distance from the start of the cycle to the meeting point is
b
. - The remainder of the cycle (back to the start of the cycle) is
c
. - When the slow pointer and fast pointer meet, the distance covered by the fast pointer is twice that of the slow pointer:
fast = 2 * slow
.- If we denote the total distance traveled by the slow pointer as
a + b
, then the fast pointer travelsa + b + b + c = 2(a + b)
. - Simplifying this, we get:
a = c
.
- Finding the Entry Point:
- After detecting the cycle, if we move a new pointer
entry
from the head and move theslow
pointer from the meeting point, both will meet at the start of the cycle aftera
steps. - This is because the distance from the head to the start of the cycle is equal to the distance from the meeting point to the start of the cycle (
a = c
).
Code Implementation in C
Complexity Analysis
- Time Complexity: O(n)
- In the worst case, we traverse each node at most twiceโonce to detect the cycle and once to find the start of the cycle. Therefore, the time complexity is linear in terms of the number of nodes.
- Space Complexity: O(1)
- This algorithm uses a constant amount of extra space, as we only need a few pointer variables.