You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

回文链表反转校验代码未通过测试用例,求分析与数组法的差异

Why Your Reversed Linked List Palindrome Check Fails (And How It Differs From The Array Method)

Hey there! Let's walk through exactly what's tripping up your code, especially with the [0,0] test case, and how it stacks up against the working array-based approach.

First, The Two Critical Issues In Your Code

1. You're Destroying The Original Linked List When Reversing

Your reversal logic modifies the actual node pointers of the original linked list. Let's break down what happens with the [0,0] test case:

  • Original list: head -> 0 (node A) -> 0 (node B) -> None
  • During reversal, the first node (A) has its next set to None (since prev starts as None). By the time you finish reversing, the original head (node A) now points to nothing—your original list is reduced to just node A -> None, while the reversed list is node B -> node A -> None.
  • When you try to compare the original list and reversed list afterward, you're not comparing the full original list anymore—it's already broken!

2. You're Comparing Node References, Not Values

Your code checks if prev == head—this compares whether prev and head are the exact same node object, not whether their values are equal. Even if two nodes have the same val, they're different objects, so == will return False here. The array method avoids this entirely by comparing the actual values stored in the array.

Why The Array Method Works

The array-based approach:

  1. Extracts all node values into a plain array (so it doesn't modify the original linked list at all).
  2. Compares the array to its reversed version using sol == sol[::-1], which checks each element's value one by one—exactly what you need for a palindrome check.

Fixed Version Of Your Reversal Approach

If you want to stick with reversing a linked list to check for palindromes, you need to either:

  • Reverse a copy of the original list (so you don't destroy the original), then compare node values.
  • Or (more efficiently) reverse only the second half of the original list and compare it to the first half.

Here's a corrected implementation using the copy approach:

def isPalindrome(head):
    # Edge case: single node or empty list
    if not head or not head.next:
        return True
    
    # Create a copy of the original linked list
    dummy = ListNode(0)
    copy_ptr = dummy
    current = head
    while current:
        copy_ptr.next = ListNode(current.val)
        copy_ptr = copy_ptr.next
        current = current.next
    copy_head = dummy.next
    
    # Reverse the copied list
    prev = None
    current = copy_head
    while current:
        nex = current.next
        current.next = prev
        prev = current
        current = nex
    
    # Compare original list and reversed copy by value
    while head and prev:
        if head.val != prev.val:
            return False
        head = head.next
        prev = prev.next
    # Both lists should be exhausted if it's a palindrome
    return head is None and prev is None

And here's the more efficient "reverse second half" method (no extra list copy needed):

def isPalindrome(head):
    # Edge case handling
    if not head or not head.next:
        return True
    
    # Find the middle of the list using slow/fast pointers
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    
    # Reverse the second half of the list
    prev = None
    current = slow
    while current:
        nex = current.next
        current.next = prev
        prev = current
        current = nex
    
    # Compare first half and reversed second half
    while prev:
        if head.val != prev.val:
            return False
        head = head.next
        prev = prev.next
    return True

内容的提问来源于stack exchange,提问作者barcaman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:33:08