141. Linked List Cycle
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 52% Topics: Hash Table, Linked List, Two Pointers
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if not head or not head.next:
return False
slow = head
fast = head.next
while slow != fast:
if not fast or not fast.next:
return False
slow = slow.next
fast = fast.next.next
return True
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions