Skip to content

Commit 7257142

Browse files
authored
Create Cyclic Detection
1 parent b532631 commit 7257142

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

interview_query/Cyclic Detection

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Given a ListNode representing the head of a linked list, write a function is_cyclic which returns True if a linked list is cyclic and False if it is not. A linked list is cyclic when there is no tail to the linked list, and the supposed tail is attached to another node inside the list itself, creating a cycle.
2+
3+
A ListNode is defined as:
4+
5+
class ListNode:
6+
value: ListNode = None
7+
next: ListNode = None
8+
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
9+
10+
The given problem is a classic example of detecting a cycle in a linked list, a common challenge in computer science known for its implications in memory management and data structure integrity. The solution employs a strategy famously known as “Floyd’s Tortoise and Hare” algorithm, which uses two pointers moving at different speeds to explore the list. This method is efficient and elegant, providing a way to detect cycles without additional memory overhead for tracking visited nodes.
11+
12+
Here’s a detailed explanation of how the solution works:
13+
14+
Initialization: Two pointers, slow and fast, are initialized at the head of the list. The slow pointer advances one node at a time, while the fast pointer moves two nodes forward with each step. This difference in speed is crucial for detecting cycles.
15+
16+
Traversal: As both pointers begin traversing the list, there are two possible outcomes:
17+
18+
If the list is acyclic (i.e., there’s an end to the list), the fast pointer will eventually reach a None value, indicating the list’s end. Since the fast pointer moves two steps at a time, it’s also necessary to check if fast.next is None to prevent accessing a None reference in the next iteration.
19+
If the list is cyclic, the fast pointer will eventually “lap” the slow pointer by entering a cycle and catching up to it from behind. This is because, in each iteration, the fast pointer reduces the gap between itself and the slow pointer by one node. Eventually, they will point to the same node within the cycle, signaling its presence.
20+
Cycle Detection: The moment slow and fast pointers meet, a cycle is confirmed, and the function returns True. If the fast pointer reaches the end of the list (fast or fast.next is None), it means there’s no cycle, and the function returns False.
21+
22+
Edge Cases: The initial check (if not head or not head.next:) ensures that the list is not empty and has more than one node. An empty list or a single-node list cannot form a cycle, so the function immediately returns False in these cases.
23+
24+
This approach is widely appreciated for its O(n) time complexity—where n is the number of nodes in the list—and O(1) space complexity.
25+
26+
Below is the implementation of the algorithm:
27+
28+
class ListNode:
29+
def __init__(self, value=0, next=None):
30+
self.value = value
31+
self.next = next
32+
33+
def is_cyclic(head: ListNode) -> bool:
34+
if not head or not head.next:
35+
return False
36+
37+
slow = head
38+
fast = head
39+
40+
while fast and fast.next:
41+
slow = slow.next # Move slow pointer by 1
42+
fast = fast.next.next # Move fast pointer by 2
43+
44+
if slow == fast:
45+
return True # Cycle detected
46+
47+
return False # No cycle detected
48+
This solution elegantly handles the problem, providing a quick and memory-efficient way to detect cycles in linked lists.
49+
50+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
51+
52+
class ListNode:
53+
pass # No need to define
54+
55+
56+
def is_cyclic(head: ListNode) -> bool:
57+
fast,slow = head, head
58+
59+
while fast and fast.next:
60+
61+
fast = fast.next.next
62+
slow = slow.next
63+
if fast == slow:
64+
return True
65+
return False
66+

0 commit comments

Comments
 (0)