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

Linked List Cycle

LeetCode 141 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Floyd: fast meets slow ⇔ cycle.

எளிதுHash TableLinked ListTwo PointersFloyd's Cycle Finding Algorithm
LeetCode இல் திற ↗

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

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

Intuition

Send a slow pointer one step and a fast pointer two steps. If there is a loop, the fast one eventually laps the slow one and they meet; if not, the fast pointer reaches the end.

Approach

  1. 1

    slow and fast start at head.

  2. 2

    Advance slow by 1 and fast by 2.

  3. 3

    Equal → cycle; fast hits None → no cycle.

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

Floyd's cycle detectionPythonO(n) time · O(1) space
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        slow = head
        fast = head
        while fast and fast.next!=None:
            slow = slow.next
            fast = fast.next.next
            if slow==fast:
                return True
        return False
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

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

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

Send a slow pointer one step and a fast pointer two steps. If there is a loop, the fast one eventually laps the slow one and they meet; if not, the fast pointer reaches the end.

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

1. slow and fast start at head. 2. Advance slow by 1 and fast by 2. 3. Equal → cycle; fast hits None → no cycle.

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

Time: O(n). Space: O(1). (Accepted approach: Floyd's cycle detection.)

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

Compare nodes (identity), not values.