Skip to content

Commit 7101623

Browse files
Create Linked List Cycle.py
1 parent c99944e commit 7101623

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def hasCycle(self, head: ListNode) -> bool:
9+
if not head:
10+
return False
11+
12+
slow, fast = head, head.next
13+
while slow != fast:
14+
if fast is None or fast.next is None:
15+
return False
16+
slow = slow.next
17+
fast = fast.next.next
18+
19+
return True

0 commit comments

Comments
 (0)