Question Name: Linked List Cycle
Same as the problem in Cracking the Coding Interview.
Solution to Linked List Cycle by LeetCode
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# @param head, a ListNode
# @return a boolean
def hasCycle(self, head):
if head == None: return False
slower = head # Move one step each time
quicker = head # Move two steps each time
while quicker.next != None and quicker.next.next != None:
slower = slower.next
quicker = quicker.next.next
if slower == quicker: return True
# quicker reach the end. Limited list never have cycle.