回文链表反转校验代码未通过测试用例,求分析与数组法的差异
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
nextset toNone(sinceprevstarts asNone). By the time you finish reversing, the originalhead(node A) now points to nothing—your original list is reduced to justnode A -> None, while the reversed list isnode 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:
- Extracts all node values into a plain array (so it doesn't modify the original linked list at all).
- 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

