141. Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow-Up:

Can you solve it without using extra space?

Solution: Two Pointers

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        slow = head
        fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                return True
        return False

results matching ""

    No results matching ""