All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0141_linked_list_cycle.Solution.py Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List
# #Data_Structure_I_Day_7_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1)
# #2024_06_09_Time_49_ms_(30.79%)_Space_19.1_MB_(64.48%)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        if not head:
            return False
        fast = head.next
        slow = head
        while fast and fast.next:
            if fast == slow:
                return True
            fast = fast.next.next
            slow = slow.next
        return False




© 2015 - 2025 Weber Informatics LLC | Privacy Policy