LeetCode 141: Linked List Cycle
Solving LeetCode 141: Detecting a Cycle in a Linked List

I'm Varchasv, a Data Engineer working on enterprise data integration.
Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.
What I'm doing:
- Solving 2 Leetcode problems daily (SQL + DSA).
- Blogging about each problem.
- Building in public.
My Goal - Land a better data engineering role by mid-2026.
Follow my journey !!
Date: January 28, 2026
Category: Linked List
Time Taken: 30 minutes
Difficulty: Easy
Problem Statement
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Link: Linked List Cycle
My Approach
Solution 1 (HashSet):
We will create a variable
currentto keep track of the current node and define a HashSetseento store all the nodes we have already encountered.We will run a while loop until our current node is null, meaning we have reached the end of the linked list. If this happens, it means there is no cycle, and we can return
False.While
currentis not null, we will check if the current node is already inseen. If it is, then there is a cycle. If not, we will add the current node toseenand updatecurrenttocurrent.next.
Solution Code
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
seen = set()
current = head
while current:
if current in seen:
return True
seen.add(current)
current = current.next
return False
Complexity:
Time: O(N) Space: O(N)
Solution 2 (Tortoise and Hare Algorithm) [Optimized]:
A more efficient solution uses two pointers:
fastandslow. The slow pointer moves forward by one node, while the fast pointer moves forward by two nodes. If they meet at any point other than the start, it means there is a loop.We will run a while loop until the
fastorfast.nextpointer is null. If they are null, it means there is no cycle.If
fastandfast.nexthave not reached null, we will moveslowtoslow.nextandfasttofast.next.next. Then, we will check iffast == slow. If this happens, it means there is a cycle.This is a better solution because we don’t need a HashSet to keep track of previous nodes.
Solution Code
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
# We will use Tortoise and Hare algorithm
# We will use a fast and a slow pointer
slow, fast = head, head
# While the fast pointer and the next to that don't point to null
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# If slow and fast pointer meet anywhere we have a cycle
if slow == fast:
return True
# If fast reaches null that means there is no cycle
return False
Complexity:
Time: O(N) Space: O(1)
Key Takeaway: Tortoise and Hare Algorithm.
Pattern: Linked List | Tortoise and Hare Algorithm
Series: 90 Days of Data Engineering Progress: 25/90 problems completed
Tags: #DEQuest #LeetCode #Python #DataEngineering #BuildInPublic