Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 142 · Fast & Slow Pointers

Linked List Cycle II

LeetCode 142 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Meet, reset one to head, step together → cycle start.

நடுத்தரம்Hash TableLinked ListTwo PointersFloyd's Cycle Finding Algorithm
LeetCode இல் திற ↗

நான் Linked List Cycle II (LeetCode 142) ஐ இப்படித் தீர்த்தேன் — Fast & Slow Pointers பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

After slow and fast meet inside the loop, the distance from the head to the loop entrance equals the distance from the meeting point to the entrance (going around). Restart one pointer from the head and step both by one: they meet exactly at the entrance.

Approach

  1. 1

    Phase 1: Floyd until slow == fast (or fast reaches the end → no cycle).

  2. 2

    Phase 2: fast = head.

  3. 3

    Advance both by one until they meet; return that node.

நான் எப்படித் தீர்த்தேன்

Floyd two-phasePythonO(n) time · O(1) space
class Solution:
    def detectCycle(self, head):
        slow = head
        fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                break
        if fast is None or fast.next is None:
            return None
        fast = head
        while slow != fast:
            slow = slow.next
            fast = fast.next
        return slow
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Linked List Cycle II — அடிக்கடி கேட்கப்படும் கேள்விகள்

Linked List Cycle II பின்னாலுள்ள intuition என்ன?

After slow and fast meet inside the loop, the distance from the head to the loop entrance equals the distance from the meeting point to the entrance (going around). Restart one pointer from the head and step both by one: they meet exactly at the entrance.

Linked List Cycle II (LeetCode 142) ஐ எப்படித் தீர்ப்பது?

1. Phase 1: Floyd until slow == fast (or fast reaches the end → no cycle). 2. Phase 2: fast = head. 3. Advance both by one until they meet; return that node.

Linked List Cycle II இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(1). (Accepted approach: Floyd two-phase.)

Linked List Cycle II இல் பொதுவான தவறுகள் என்ன?

Return null when phase 1 ends without meeting.