Palindrome Linked List (LeetCode 234): Solution and Explanation
Given the head of a singly linked list, decide whether its values read the same from front to back as from back to front.
Watch it run step by step
Try your own input and write the code →Intuition
With an array this would be easy: put one pointer at each end and walk inward comparing values. A linked list only goes forward, so the simplest fix is to copy the values into a list and run that check. It is O(n) time but O(n) extra space, and the classic follow-up asks you to do it in constant space.
The trouble is the second half: you need to read it backward. You cannot walk a singly linked list backward, but you can reverse it in place in O(1) extra space by flipping each next pointer. If you reverse just the second half, then walking forward from its new head reads the original list's tail end first, which is exactly what you want to compare against the front.
To reverse the second half, you first have to find where it starts, without knowing the length. Fast and slow pointers do that in one pass: the fast pointer moves two nodes per step and the slow one moves one, so when fast reaches the end, slow sits at the middle. For an even-length list it lands on the first node of the second half; for an odd length it lands on the exact middle node, which is fine to include because a single middle value always matches itself.
After reversing from slow, compare values node by node from head and from the reversed half's head. The reversed half is never longer than the first half, so you stop when it runs out. Any mismatch means the list is not a palindrome.
Approach: Fast and slow pointers plus in-place reversal
- Set
slowandfasttohead, and whilefastandfast.nextexist, moveslowone step andfasttwo steps. - When the loop stops,
slowis at the start of the second half (or the exact middle for odd lengths). - Reverse the list starting at
slowwith the usualprev,slow,tmppointer flip, soprevends as the head of the reversed half. - Walk
leftfromheadandrightfromprevtogether. - If any pair of values differs, return false; when
rightruns out, return true.
Step-by-step example
head = [1,2,2,1]
- Both pointers start at the first 1. After one step,
slowis at the first 2 andfastis at the second 2. fast.nextis the last 1, so they move again:slowreaches the second 2 andfastbecomesNone. The second half starts at the second 2.- Reversing 2, 1 gives 1, 2, so
prevpoints at the last node, whose value is 1. - Compare 1 with 1, then 2 with 2. Both match.
rightreaches the end of the reversed half, so the function returns true.
Code: Python, Java, C++
1class Solution:
2 def isPalindrome(self, head: Optional[ListNode]) -> bool:
3 slow = fast = head
4 while fast and fast.next:
5 slow = slow.next
6 fast = fast.next.next
7
8 prev = None
9 while slow:
10 tmp = slow.next
11 slow.next = prev
12 prev = slow
13 slow = tmp
14
15 left, right = head, prev
16 while right:
17 if left.val != right.val:
18 return False
19 left = left.next
20 right = right.next
21 return True1class Solution {
2 public boolean isPalindrome(ListNode head) {
3 ListNode slow = head, fast = head;
4 while (fast != null && fast.next != null) {
5 slow = slow.next;
6 fast = fast.next.next;
7 }
8
9 ListNode prev = null;
10 while (slow != null) {
11 ListNode tmp = slow.next;
12 slow.next = prev;
13 prev = slow;
14 slow = tmp;
15 }
16
17 ListNode left = head, right = prev;
18 while (right != null) {
19 if (left.val != right.val) return false;
20 left = left.next;
21 right = right.next;
22 }
23 return true;
24 }
25}1class Solution {
2public:
3 bool isPalindrome(ListNode* head) {
4 ListNode* slow = head;
5 ListNode* fast = head;
6 while (fast && fast->next) {
7 slow = slow->next;
8 fast = fast->next->next;
9 }
10
11 ListNode* prev = nullptr;
12 while (slow) {
13 ListNode* tmp = slow->next;
14 slow->next = prev;
15 prev = slow;
16 slow = tmp;
17 }
18
19 ListNode* left = head;
20 ListNode* right = prev;
21 while (right) {
22 if (left->val != right->val) return false;
23 left = left->next;
24 right = right->next;
25 }
26 return true;
27 }
28};Time and space complexity
O(n)
Finding the middle takes about n / 2 steps, reversing the second half takes about n / 2, and the comparison takes at most n / 2, so the total is linear.
O(1)
Only a handful of pointers are used. The reversal rewires existing nodes instead of allocating new ones.
Other approaches
Copy values into an array (time O(n), space O(n))
Walk the list once, append each value to a Python list, and check whether it equals its reverse. It is short and leaves the input untouched, but uses memory proportional to the list length.
Recursion with a front pointer (time O(n), space O(n))
Recurse to the end of the list, and while unwinding compare each node with a front pointer that advances on every return. It reads the list backward through the call stack, so it needs O(n) stack space and can overflow on long lists.
Common mistakes
- Looping while
leftis notNoneinstead of whilerightis notNone. The first half still links into the tail of the reversed part, so the lengths differ, and the reversed half is the right one to bound the loop. - Writing the fast pointer condition as
while fast.next and fast.next.nextwithout checkingfastfirst, which crashes on an empty list. - Losing the rest of the list during reversal by overwriting
slow.nextbefore saving it intmp. - Leaving the list modified when the caller expects it intact. If that matters, reverse the second half back before returning.
Frequently asked questions
How do you check if a linked list is a palindrome in O(1) space?
Find the middle with fast and slow pointers, reverse the second half in place, and compare it node by node with the first half. Only a few pointers are needed.
Does this solution modify the original linked list?
Yes. The second half is left reversed. If the list must be preserved, reverse that half again after the comparison, which keeps the time O(n) and space O(1).
How does it handle odd-length lists?
For an odd length the slow pointer stops on the exact middle node, which ends up at the tail of the reversed half. It gets compared with itself, so it never causes a mismatch.