Middle of the Linked List

Tortoise and Hare algorithm with slow/fast pointers

Read Here
Step0 / 8
PhaseInitialize
Nodes5
Middle1
Back To Linked List

Tortoise & Hare (Python)

2 # Initialize both pointers at head
slow = head
4 fast = head
5
6 # Move until fast reaches end
7 while fast and fast.next:
8 slow = slow.next # Move 1 step
9 fast = fast.next.next # Move 2 steps
10
11 # slow is now at middle
12 return slow
Current Line (3): Set slow pointer

Tortoise & Hare

INIT

Step

0 / 8

Phase

Initialize

Slow Pointer

1

null1next2next3next4next5nextslowfast

Algorithm Progress

Middle Node

1

Phase

Initialize

Pointer Positions

Slow (Tortoise)

1

Fast (Hare)

1

Algorithm Insight

The slow pointer moves 1 step while fast moves 2 steps. When fast reaches the end, slow is at the middle. This runs in O(n) time with O(1) space.

Press "Start" to initialize slow and fast pointers at head.

Pointer State

slow1
fast1

Why This Step Matters

Both pointers start at head. Slow moves 1 step, fast moves 2 steps per iteration.

Current Action

Initialize: slow = head, fast = head

Step Explanation

Setup

Initialize Pointers

Set both slow and fast pointers to the head of the list.

  • > Phase: Setup
  • > Both pointers start at the same position
  • > This is the starting point of the algorithm
  • > Both pointers start at the same position.
Unvisited
Current (Slow)
Done
Fast Pointer