#141
Linked List Cycle
easy· Linked Listruns: 0Given the head of a linked list, determine if the list contains a cycle. A cycle exists when a node can be reached again by continuously following the next pointer.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 273
class Solution:
def hasCycle(self, head: ListNode | None) -> bool:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.