Skip to main content

Linked List Cycle

Question

None

Example 1
Input: head = [3,2,0,-4], pos = 1
Output: true

Solution

all//Linked List Cycle.py


# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None


class Solution:
def hasCycle(self, head):
slow = head
fast = head
while slow and fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False