-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.py
More file actions
36 lines (28 loc) · 1011 Bytes
/
ListNode.py
File metadata and controls
36 lines (28 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
def has_cycle(head):
if not head or not head.next:
# No cycle if the list is empty or has only one node
return False
slow = head
fast = head
while fast and fast.next:
slow = slow.next # Move slow pointer by one step
fast = fast.next.next # Move fast pointer by two steps
if slow == fast:
# If slow and fast pointers meet, there is a cycle
return True
return False
# Example usage
head1 = ListNode(3)
head1.next = ListNode(2)
head1.next.next = ListNode(0)
head1.next.next.next = ListNode(-4)
head1.next.next.next.next = head1.next # Create a cycle
head2 = ListNode(1)
head2.next = ListNode(2)
print("Example 1 (has cycle):", has_cycle(head1)) # Output: True
print("Example 2 (has cycle):", has_cycle(head2)) # Output: False
print("Example 3 (no cycle):", has_cycle(ListNode(1)))