MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Fast & Slow Pointers: Floyd's Cycle Detection & Middle Node Finding0%
Study Materials
Data Structures & AlgorithmsProgress 0%
1.1Introduction to DSA, Big-O, Big-Omega & Big-Theta Notations1.2Time Complexity Calculation Rules & Common Runtime Patterns1.3Space Complexity, Auxiliary Memory & Stack vs Heap Allocation
3.1Singly Linked Lists: Node Architecture, Traversal & In-Place Reversal3.2Doubly & Circular Linked Lists with Sentinel/Dummy Nodes3.3Fast & Slow Pointers: Floyd's Cycle Detection & Middle Node Finding
HomeTutorialsData Structures & AlgorithmsFast & Slow Pointers: Floyd's Cycle Detection & Middle Node Finding
Stacks: LIFO Operations, Array/List Implementations & Bracket Matching

Fast & Slow Pointers: Floyd's Cycle Detection & Middle Node Finding

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

The Fast and Slow Pointer Technique (also celebrated as Floyd's Tortoise and Hare Algorithm) is a two-pointer pattern where two pointers traverse a sequence at different speeds. It is the gold standard for detecting loops in linked lists, locating the middle node, and finding the entrance of a cycle in $O(n)$ time and $O(1)$ space.


1. Real-World Analogy: Runners on a Track

Imagine two runners, a Tortoise and a Hare, running on an Olympic running track:

  • Straight Road (No Cycle): The fast runner (Hare) reaches the finish line and stops. They never cross paths again.
  • Circular Track (Cycle Exists): Because the track loops, the faster runner running at 2 meters/sec will inevitably lap and collide with the slower runner running at 1 meter/sec!
1Visualizing Floyd's Tortoise and Hare on a Circular Track:
2Slow: moves 1 step per tick (Tortoise 🐢)
3Fast: moves 2 steps per tick (Hare 🐇)
4If Fast meets Slow (Slow == Fast), a cycle is mathematically guaranteed to exist!

2. Problem 1: Find the Middle Node of a Linked List in a Single Pass

In an array, finding the middle is trivial: arr[n // 2]. In a linked list where the total length $n$ is unknown:

  • Initialize slow = head and fast = head.
  • Move slow by 1 step (slow = slow.next).
  • Move fast by 2 steps (fast = fast.next.next).
  • When fast reaches the end (fast is None or fast.next is None), slow is precisely at the middle node!

Python 3:

1def find_middle_node(head: ListNode) -> ListNode:
2 slow = head
3 fast = head
4
5 while fast is not None and fast.next is not None:
6 slow = slow.next # Moves 1 step
7 fast = fast.next.next # Moves 2 steps
8
9 return slow # When fast hits the end, slow is at the middle!

3. Problem 2: Floyd's Cycle Detection Algorithm

Detect if a linked list contains a cycle:

Modern JavaScript / TypeScript:

1export function hasCycle(head: ListNode | null): boolean {
2 let slow: ListNode | null = head;
3 let fast: ListNode | null = head;
4 
5 while (fast !== null && fast.next !== null) {
6 slow = slow.next!;
7 fast = fast.next.next;
8 
9 // Collision detected! A cycle exists.
10 if (slow === fast) {
11 return true;
12 }
13 }
14 
15 return false; // Fast reached NULL, no cycle exists
16}

4. Problem 3: Finding the Exact Cycle Entry Node (Floyd's Phase 2)

Once slow and fast collide at point $C$, how do you find the exact node where the cycle began?

  1. 1
    Keep slow at the collision meeting point.
  2. 2
    Reset fast back to the head of the linked list.
  3. 3
    Advance both slow and fast at the same speed of 1 step per tick.
  4. 4
    The exact node where they collide again is the Entry Node of the Cycle!
1def detect_cycle_entry(head: ListNode) -> ListNode:
2 slow = fast = head
3 has_cycle = False
4
5 # Phase 1: Detect Collision
6 while fast and fast.next:
7 slow = slow.next
8 fast = fast.next.next
9 if slow == fast:
10 has_cycle = True
11 break
12
13 if not has_cycle:
14 return None
15
16 # Phase 2: Locate Entry
17 fast = head
18 while slow != fast:
19 slow = slow.next
20 fast = fast.next # Both move at 1 step per tick!
21
22 return slow # Both meet at the cycle entry node

5. Trace Table: Fast & Slow Middle Finding on 1 -> 2 -> 3 -> 4 -> 5 -> NULL

Stepslow Nodefast Nodefast.nextLoop Condition (fast && fast.next)
Start112True
1234True
23 (Middle!)5NULLFalse (Loop terminates)

Result: slow stops at Node 3 in exactly $\frac{n}{2}$ steps without needing a counter or second pass!


Interactive Quiz• Knowledge Check

Multiple Choice Questions

Question 1
In Floyd's Cycle Detection Algorithm, at what speeds do the two pointers traverse the linked list?
Question 2
What is the space complexity of Floyd's Cycle Finding Algorithm compared to using a Hash Set of visited nodes?
Question 3
How does the Fast & Slow pointer pattern locate the middle node of a linked list of length $n$ in a single traversal?
Question 4
In Floyd's Algorithm Phase 2, how do you locate the start node of the cycle once a collision has occurred?
Question 5
What condition must be checked in the while loop of the Fast & Slow pointer algorithm to prevent null pointer exceptions?

Next Lesson

Stacks: LIFO Operations, Array/List Implementations & Bracket Matching

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Doubly & Circular Linked Lists with Sentinel/Dummy NodesStacks: LIFO Operations, Array/List Implementations & Bracket Matching

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Data Structures & Algorithms with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext