Palindrome Linked List
Pattern: Fast and slow pointers
Find the middle with slow and fast pointers, reverse the second half, and compare it node by node with the first half. O(n) time and O(1) extra memory.
The problem
Given the head of a singly linked list, return whether its values read the same forwards and backwards. For example,
1 → 2 → 2 → 1 gives true and 1 → 2 gives false. LeetCode asks whether it can be done in O(n) time and O(1) extra
memory.
The function name isPalindrome matches LeetCode. Use the link above for the exact statement and limits. The method
below changes the list's second half; if the caller needs the list intact, reverse it back before returning.
Think first
- With an array you would compare the two ends. Why is that hard on a singly linked list?
- How can you walk the second half backwards?
- Where does the second half start?
Hints
Brute force, and why it fails
Copy the values into an array and check it with two indexes: O(n) time and O(n) memory. A stack of the first half also needs O(n). Reversing the second half needs O(1).
The key insight
A palindrome compares the first half with the second half backwards. Reversing the second half in place gives you a forward walk over it, so two simple pointers can compare.
Dry run
The list is 1 → 2 → 2 → 1. slow and fast find the middle. slow ends on index 2, the start of the second half.
The plan
slow = fast = head
while fast and fast.next: slow = slow.next; fast = fast.next.next
prev = reverse the list starting at slow
a = head, b = prev
while b is not null:
if a.val != b.val: return false
a = a.next; b = b.next
return true
Complexity
- Time
O(n)- Space
O(1)
Finding the middle, reversing and comparing are each one pass.
Edge cases
- One node: true.
- Odd length, as in
1 → 2 → 1: the middle node ends up compared with itself, which always matches. - Two different values: false at the first comparison.
I've built my logic, show the code
Java
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } // slow: middle
ListNode prev = null; // reverse the second half
while (slow != null) {
ListNode next = slow.next;
slow.next = prev;
prev = slow;
slow = next;
}
for (ListNode a = head, b = prev; b != null; a = a.next, b = b.next) { // compare the halves
if (a.val != b.val) return false;
}
return true;
}
}C++
class Solution {
public:
bool isPalindrome(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; } // slow: middle
ListNode* prev = NULL; // reverse the second half
while (slow != NULL) {
ListNode* next = slow->next;
slow->next = prev;
prev = slow;
slow = next;
}
for (ListNode *a = head, *b = prev; b != NULL; a = a->next, b = b->next) { // compare the halves
if (a->val != b->val) return false;
}
return true;
}
};Python
from lib import ListNode
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
slow = fast = head
while fast is not None and fast.next is not None: # slow ends on the middle
slow, fast = slow.next, fast.next.next
prev = None # reverse the second half
while slow is not None:
slow.next, prev, slow = prev, slow, slow.next
a, b = head, prev
while b is not None: # compare the halves
if a.val != b.val:
return False
a, b = a.next, b.next
return TrueCommon mistakes
- Comparing until
ais null, which walks into the reversed half after the first half ends. - Reversing the whole list, which then compares the list with itself.
- Forgetting that the list is changed afterwards.