Last Updated: March 29, 2026
A palindrome reads the same forwards and backwards. With an array, you would just compare elements from both ends moving inward. But a singly linked list only lets you traverse in one direction, so you can't easily walk backwards from the tail.
That's the core challenge here: how do you compare elements from the front and back of a structure that only supports forward traversal? There are a few ways to work around this limitation, each with different trade-offs between simplicity, time, and space.
The number of nodes is in the range [1, 10^5] → With up to 100,000 nodes, O(n^2) approaches would be borderline. O(n) is clearly preferred.0 <= Node.val <= 9 → Values are single digits. This is a small detail but means we don't need to worry about overflow or large value comparisons.The simplest way to handle the "can't traverse backwards" problem: just copy all the values into an array, then use the classic two-pointer palindrome check. Arrays support random access, so comparing from both ends is straightforward.
This converts a linked list problem into an array problem, which is a useful technique when the list's constraints (no random access, no backward traversal) are getting in the way.
left starting at index 0 and right starting at the last index.left and right. If they differ, return false.left forward and right backward. Repeat until they meet or cross.true.This approach works correctly but uses extra space. Is there a way to compare the first half and second half of the list without copying everything into an array?
Here's the key insight: if we find the middle of the linked list, we can reverse the second half in-place, then compare both halves node by node. No extra array needed.
Finding the middle is a classic technique: use a slow pointer (moves 1 step at a time) and a fast pointer (moves 2 steps at a time). When fast reaches the end, slow is at the middle. Then we reverse the list from slow onward and walk both halves simultaneously, comparing values.
The slow/fast pointer technique guarantees that slow lands at the middle (or just past it for odd-length lists). After reversing from that point, the reversed half has exactly as many nodes as the first half (or one fewer for odd lists). Comparing node by node from both ends tells us if the list is a palindrome.
For odd-length lists like [1, 2, 3, 2, 1], slow ends up at the middle node (3). Reversing from there gives us 1 → 2 → 3. We compare 1,2 from the first half against 1,2 from the reversed half. The middle element (3) doesn't need comparison since it pairs with itself.
false. If all match, return true.