↓Skip to main content
  1. LeetCode/

LeetCode 142: Linked List Cycle II

·2 mins· ·
LeetCode Medium 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: medium First Attempt: 2022-11-14

  • Submissions: Accepted ×2

Approach
#

First find the meeting point with fast and slow pointers (Linked List Template 3). Let a be head-to-entry, b entry-to-meeting point, and c the cycle length: slow traveled a + b and fast twice that, so the extra a + b is a multiple of c, giving a = k·c - b.

So walking a more steps from the meeting point returns to the entry. Send one pointer back to head and step both one at a time; they arrive at the entry together.

My Solution
#

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = fast = head
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
            if slow is fast:
                slow = head
                while slow is not fast:
                    slow, fast = slow.next, fast.next
                return slow
        return None

Complexity
#

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

Mistakes to Avoid
#

  • In the second phase both pointers step one at a time.
  • A set of visited nodes also works, but uses O(n) space.

Interview Explanation
#

Fast and slow pointers find a meeting point; the math shows head-to-entry equals meeting-point-to-entry modulo the cycle length, so one pointer restarts from the head and both step one at a time until they meet at the entry. Time O(n), space O(1).

Related

LeetCode 141: Linked List Cycle
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 141 note: Floyd’s cycle detection, fast two steps and slow one; with a cycle they must meet, each advancing from its own position
LeetCode 61: Rotate List
·1 min
LeetCode Medium Linked-List Two-Pointers
LeetCode 61 note: count the length and close a ring, take k %= n, walk n - k - 1 steps from the head to the new tail, and break there
LeetCode 138: Copy List with Random Pointer
·2 mins
LeetCode Medium Hash-Table Linked-List
LeetCode 138 note: the first pass maps old nodes to new ones, the second wires next and random; seed the dictionary with None -> None
LeetCode 143: Reorder List
·2 mins
LeetCode Medium Linked-List Two-Pointers Stack Recursion
LeetCode 143 note: find the middle, reverse the second half, and merge the two alternately, cutting the first half’s tail
LeetCode 82: Remove Duplicates from Sorted List II
·2 mins
LeetCode Medium Linked-List Two-Pointers
LeetCode 82 note: every duplicated value goes, the head included, so use a dummy; on a duplicate skip to the next different value and link prev.next past it
LeetCode 86: Partition List
·2 mins
LeetCode Medium Linked-List Two-Pointers
LeetCode 86 note: two dummies collect nodes below x and at least x, then join them, setting the second part’s tail to None