Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created linkedlist_hasCycle.py #2042

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions DS&Algo Programs in Python/linkedlist_hasCycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from typing import Optional

class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
def hasCycle(self,head:Optional[ListNode])->bool:

#if no linkedlist or a single node linkedlist
if head is None or head.next is None:
return False

slow=head
fast=head

while slow!=fast:
if fast is None or fast.next is None:
return False

slow=slow.next #goes to next node
fast=fast.next.next #goes to next to next node

return True

# Create some nodes for a linked list
node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node4 = ListNode(4)
node5 = ListNode(5)
node6 = ListNode(6)
node7 = ListNode(7)

# Link the nodes together
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node6
node6.next = node7

# Optionally create a cycle
# Uncomment the line below to create a cycle
node6.next = node3

# Create an instance of the Solution class
solution = Solution()

# Check if the linked list has a cycle
result = solution.hasCycle(node1)

# Print the result
print("Cycle detected!" if result else "No cycle detected")