⚡Flashcard Drill➔
NeetCode #306LC-234EasyLinked List
← Back to All Problems

#306 · #234 · Palindrome Linked List(回文链表)

📌 Problem Statement & Constraints

Given the head of a singly linked list, return true if it is a palindrome. Constraints: the number of nodes is in [1, 10^5], 0 <= Node.val <= 9. The follow-up asks for O(n) time and O(1) space.

💡 Core Algorithmic Approaches

  1. Three steps: find the middle, reverse the second half, then compare the two halves node by node.
  2. For an odd-length list, the middle node is shared and can be ignored; the comparison loop naturally stops when the reversed half is exhausted.
  3. This achieves O(n) time and O(1) extra space, unlike copying the values into a list.
  4. Optionally restore the list afterwards, which a tidy implementation would do.

💻 Benchmark Python3 Implementation

class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        # 1) find the middle
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        # 2) reverse the second half
        prev, cur = None, slow
        while cur:
            cur.next, prev, cur = prev, cur, cur.next
        # 3) compare
        left, right = head, prev
        while right:                       # the second half is no longer than the first
            if left.val != right.val:
                return False
            left = left.next
            right = right.next
        return True

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n): three linear passes.
💾 Space Complexity
O(1): only pointers are used.

⚠️ Interview Pitfalls & Follow-ups

  • Copying the values into a list and using two pointers: correct and simple, but O(n) space, which the follow-up forbids.
  • Comparing the whole first half: the loop must be driven by the second (reversed) half, since it is shorter for odd lengths.
  • Forgetting the odd-length middle: it is shared between the halves but the comparison loop ignores it naturally.
  • Not restoring the list: some interviewers ask for it; it is a one-line extra reversal.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Linked List Problems

中文速览与面试要点

回文链表

给你一个单链表的头节点 head,请你判断该链表是否为回文链表。如果是,返回 true;否则,返回 false。进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?约束:链表中节点数目在 [1, 10^5] 内;0 ≤ Node.val ≤ 9。

核心要点

  • 快慢指针 + 反转后半 + 比较(O(1) 空间,标准解) :① 快慢指针找中点;② 反转后半部分;③ 从头与反转后的后半同步比较。
  • 找中点 : while fast and fast.next 使 slow 停在后半的第一个节点(奇数长度时为正中节点)。
  • 为什么可以「破坏性」反转 :题目不要求保留原链表;若要求保留,比较完再反转回来即可。
  • 替代方案 :把链表转成数组后用双指针比较,O(n) 时间、O(n) 空间,代码更简单但空间不达标。

常见陷阱

  • 比较循环以 p1 为终止条件 :奇数长度时前半比后半多一个节点,用 p1 会多比较一次并可能访问到已反转的节点。应以 p2 (后半)为终止条件。
  • 快慢指针条件写成 while fast.next and fast.next.next :会让 slow 停在前半的最后一个节点(用于 143 题的断开),本题需要 slow 停在后半第一个节点,因此用 while fast and fast.next 。
  • 反转后忘记恢复链表 :题目不要求,但若在同一函数中后续还要用原链表,则必须恢复。
  • 用「转数组后比较」 :正确但空间 O(n)。

参考实现

class Solution:
    def isPalindrome(self, head: 'ListNode') -> bool:
        # ① 快慢指针找中点(slow 停在链表中间)
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        # ② 反转后半部分
        prev = None
        cur = slow
        while cur:
            nxt = cur.next
            cur.next = prev
            prev = cur
            cur = nxt

        # ③ 比较前半与反转后的后半
        p1, p2 = head, prev
        while p2:                       # 后半更短(或等长),以它为终止条件
            if p1.val != p2.val:
                return False
            p1 = p1.next
            p2 = p2.next
        return True