Palindrome Linked List
Palindrome Linked List is LeetCode 234 (Easy). Given the head of a singly linked list, return true if its values read the same from front to back as from back to front.
- The list has at least one node, and a single node is a palindrome.
- A singly linked list can only be walked forwards, so the back of the list cannot be read directly the way an array can.
- The follow-up asks for O(n) time and O(1) extra space, which rules out copying the values.
- The number of nodes in the list is in the range [1, 10⁵].
- 0 <= Node.val <= 9
Intuition
A palindrome is a list whose first half mirrors its second half. The trouble is that the second half can only be read forwards, while the comparison needs it backwards.
So reverse the second half in place, and it reads backwards for free. Then walk one pointer from the head and one from the far end, comparing values until the reversed half runs out.
- Finding the middle: a slow pointer moving one node and a fast pointer moving two. When fast runs out, slow is at the start of the second half.
- Odd length: the middle node lands in the reversed half and is compared with itself, which always matches.
"Compare the front of a linked list with its back" in O(1) space means: find the middle with slow and fast pointers, reverse one half, and walk the two halves together.
Approach
Before reading on, ask what stops you comparing the first and last nodes directly, then which half you could change so it can be read the other way. Aim for O(n) time and O(1) extra space.
Two ways to compare the ends
Relink the back half so it can be walked from the end, compare, then relink it back.
- Meets the follow-up: only a few pointers, whatever the length.
- Touches the list: it changes
nextpointers for a moment, so it is not safe if another thread reads the list at the same time.
Read every value into an array, then close in with two indices.
- Simplest to write: no pointer surgery, nothing to undo.
- Costs memory: up to 10⁵ extra values.
The usual first answer before the O(1) follow-up.
Both read each node a constant number of times. Reversing half is the version traced below; the array copy follows the demo.
Find the middle with slow and fast pointers
Start both at the head. While fast and fast.next exist, move slow one node and fast two. Fast covers the list twice as quickly, so when it stops, slow has reached the first node of the second half.
Reverse the second half
Reverse the nodes from slow to the end with the usual three-pointer loop. It returns the old last node, which is now the start of a chain that runs back towards the middle.
Compare the two halves
Walk left from the head and right from the reversed start, one node each per round. Stop at the first pair of unequal values. Loop on right because the reversed half is never longer than the first half plus the middle node.
Reverse the second half back
Reverse the same chain again before returning. The caller handed over the list and may use it afterwards, so leaving half of it backwards would be a hidden side effect.
Code in Python, C++ and Java
Copy the values into an array
Walk the list once and store each value. An array can be read from both ends, so two indices move towards each other and stop at the first mismatch.
Common pitfalls
Returning before the list is put back
if left.val != right.val:
return Falseif left.val != right.val:
ok = False
breakAn early return skips the second reversal. For [1, 2, 3, 4] the first pair already differs, so the function returns with 4 pointing back at 3 and 3 pointing at nothing. Reading the list from the head afterwards gives only 1 → 2 → 3.
Looping on left instead of right
while left:
while right:
The first half still points into the reversed half, so left has more nodes to visit than right. For [1, 2, 2, 1] the right side runs out after two rounds while left is still on the second 2, and reading right.val on null crashes.
Edge cases
The fast pointer cannot move, so the second half is that one node. It is compared with itself and the answer is true.
Complexity
Palindrome Linked List FAQ
Can recursion check it without reversing anything?
Yes. Recurse to the end of the list and compare each node on the way back with a front pointer that moves forward. It leaves the list untouched, but the call stack holds n frames, so it is O(n) space and can overflow at 10⁵ nodes.