↓Skip to main content
  1. LeetCode/

LeetCode 141: Linked List Cycle

·2 mins· ·
LeetCode Easy Hash-Table Linked-List Two-Pointers Floyds-Cycle-Finding-Algorithm
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-20

  • Submissions: Time Limit Exceeded ×3 → Wrong Answer → Accepted → Runtime Error ×3 → Wrong Answer → Runtime Error → Wrong Answer → Runtime Error ×2 → Accepted ×2 → Runtime Error ×3 → Accepted

Approach
#

With a cycle, once both are inside it the fast pointer closes the gap by one each step and must catch up; without one, fast reaches None first (Linked List Template 3).

My Attempts
#

This took 19 submissions in 2022:

  • TLE: I wrote slow = head.next and fast = head.next.next, recomputing from head every time, so both pointers stayed put; a cycle on [3, 3] never finished.
  • RE: reading None.next on empty or acyclic lists, e.g. [] and [3, 3, 5].
  • Using while fast and fast.next and advancing each pointer from its own position passed.

Solution
#

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        slow = fast = head
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
            if slow is fast:
                return True
        return False

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Each step uses slow.next and fast.next.next, not recomputed from head.
  • Empty and single-node lists are stopped by the while condition.

Interview Explanation
#

Fast moves two steps and slow one; with a cycle fast must catch slow inside it, and fast reaching None means no cycle. Time O(n), space O(1).

Related

LeetCode 160: Intersection of Two Linked Lists
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers
LeetCode 160 note: each pointer walks its own list then the other’s; both cover lenA + lenB steps and reach the intersection, or None, together
LeetCode 169: Majority Element
·2 mins
LeetCode Easy Array Hash-Table Divide-and-Conquer Sorting
LeetCode 169 note: Boyer-Moore voting, where different values cancel in pairs and the survivor is the one appearing more than half the time
LeetCode 125: Valid Palindrome
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 125 note: two pointers skip non-alphanumeric characters and compare lowercase letters, with digits included
LeetCode 83: Remove Duplicates from Sorted List
·2 mins
LeetCode Easy Linked-List
LeetCode 83 note: sorted, so duplicates are adjacent; skip cur.next while it equals cur, and advance only otherwise
LeetCode 27: Remove Element
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 27 note: the write pointer copies only values not equal to val, returning how many were written
LeetCode 28: Find the Index of the First Occurrence in a String
·1 min
LeetCode Easy Two-Pointers String String-Matching Z-Algorithm
LeetCode 28 note: try matching needle at every start, or use str.find; index raises when not found