💡 Cycle length counting continues as slow moves through the cycle nodes.
traverse
Increment length counter and move slow one step again
Slow pointer moves from node 0 to node -4, incrementing length to 3. Slow still not equal to fast.
💡 Continuing to traverse the cycle to count all nodes in it.
Line:length += 1
slow = slow.next
💡 Counting all nodes in the cycle by moving slow pointer.
compare
Slow meets fast again, cycle length counting complete
Slow pointer equals fast pointer again at node -4, ending the cycle length count.
💡 Meeting again means one full cycle traversal is done, confirming the cycle length.
Line:while slow != fast:
...
return length
💡 Cycle length is the number of nodes traversed in the cycle.
reconstruct
Return the cycle length
The function returns the cycle length 3 as the final answer.
💡 Returning the computed cycle length completes the algorithm.
Line:return length
💡 The cycle length is the number of nodes in the detected cycle.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def cycle_length(head):
if not head:
return 0 # STEP 1
slow = fast = head # STEP 1
while fast and fast.next: # STEP 2
slow = slow.next # STEP 2
fast = fast.next.next # STEP 2
if slow == fast: # STEP 5
length = 1 # STEP 5
slow = slow.next # STEP 6
while slow != fast: # STEP 7-9
length += 1 # STEP 7-8
slow = slow.next # STEP 7-8
return length # STEP 10
return 0 # STEP 3
# Example usage:
if __name__ == '__main__':
node1 = ListNode(3)
node2 = ListNode(2)
node3 = ListNode(0)
node4 = ListNode(-4)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node2
print(cycle_length(node1)) # Output: 3
📊
Linked List Cycle Length - Watch the Algorithm Execute, Step by Step
Watching the pointers move step-by-step reveals how the fast pointer laps the slow pointer inside the cycle, and how the cycle length is counted by a second traversal.
Step 1/10
·Active fill★Answer cell
setup
3
→
2
→
0
→
-4
advance
3
→
2
→
0
→
-4
advance
3
→
2
→
0
→
-4
compare
3
→
2
→
0
→
-4
insert
3
→
2
→
0
→
-4
advance
3
→
2
→
0
→
-4
Result: 1
advance
3
→
2
→
0
→
-4
Result: 2
advance
3
→
2
→
0
→
-4
Result: 3
compare
3
→
2
→
0
→
-4
Result: 3
reconstruct
3
→
2
→
0
→
-4
Result: 3
Key Takeaways
✓ The fast pointer moves twice as fast as the slow pointer and will eventually meet slow inside the cycle if one exists.
This insight is hard to see from code alone because the meeting point depends on relative speeds and cycle structure.
✓ Once the cycle is detected, counting the cycle length requires traversing the cycle once starting from the meeting point.
Understanding that the cycle length is the number of nodes in one full loop is easier when visualized step-by-step.
✓ If fast or fast.next becomes null, it means no cycle exists and the function returns 0 immediately.
This early exit condition is critical but subtle in code; visualization shows why traversal stops.
Practice
(1/5)
1. You are given a singly linked list and need to determine if it reads the same forwards and backwards. Which approach guarantees an optimal solution with O(n) time and O(1) space complexity?
easy
A. Use fast and slow pointers to find the middle, reverse the second half in-place, then compare both halves.
B. Convert the linked list to an array and check palindrome by comparing elements from both ends.
C. Use a hash set to store visited nodes and check for palindrome by comparing values.
D. Use a recursive approach to compare nodes from start and end simultaneously.
Solution
Step 1: Identify the problem constraints
The problem requires checking palindrome in O(n) time and O(1) space.
Step 2: Evaluate approaches
Converting to array uses O(n) space, recursion uses O(n) stack space, and hash set is not suitable for palindrome check. Using fast and slow pointers to reverse second half in-place meets both time and space requirements.
Final Answer:
Option A -> Option A
Quick Check:
Optimal approach uses fast-slow pointers and in-place reversal [OK]