Tensorum
YouTubeশেখা শুরু করুন
LeetCode 234 · Palindrome

Palindrome Linked List

LeetCode 234 · সহজ · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Middle → reverse second half → compare.

সহজLinked ListTwo PointersStackRecursion
LeetCode-এ খুলুন ↗

আমি Palindrome Linked List (LeetCode 234) এভাবে সমাধান করেছি — Palindrome প্যাটার্নের একটি সহজ প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Find the middle with slow/fast pointers, reverse the second half, and compare it against the first half node by node. Reverse it back afterwards to leave the list unchanged.

Approach

  1. 1

    Slow/fast to find the middle.

  2. 2

    Reverse from slow to the end.

  3. 3

    Walk both halves comparing values.

  4. 4

    Reverse the second half back.

আমি কীভাবে সমাধান করেছি

Reverse second halfPythonO(n) time · O(1) space
class Solution:
    def reverseList(self, temp):
        prev = None
        while temp is not None:
            front = temp.next
            temp.next = prev
            prev = temp
            temp = front
        return prev

    def isPalindrome(self, head):
        if head is None:
            return False
        slow = head
        fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        p1 = head
        slow = self.reverseList(slow)
        p2 = slow
        is_palindrome = True
        while p1 is not None and p2 is not None:
            if p1.val != p2.val:
                is_palindrome = False
            p1 = p1.next
            p2 = p2.next
        slow = self.reverseList(slow)
        return is_palindrome
স্টেপ-থ্রু লোড হচ্ছে…

Palindrome Linked List — সাধারণ প্রশ্ন

Palindrome Linked List-এর পেছনে intuition কী?

Find the middle with slow/fast pointers, reverse the second half, and compare it against the first half node by node. Reverse it back afterwards to leave the list unchanged.

Palindrome Linked List (LeetCode 234) কীভাবে সমাধান করবেন?

1. Slow/fast to find the middle. 2. Reverse from slow to the end. 3. Walk both halves comparing values. 4. Reverse the second half back.

Palindrome Linked List-এর time আর space complexity কত?

Time: O(n)। Space: O(1)। (Accepted approach: Reverse second half।)

Palindrome Linked List-এ সাধারণ ভুলগুলো কী?

Restoring the list is good practice — callers may reuse it.