Palindrome Linked List (LeetCode 234): Solution and Explanation

Easy
Linked List
Two Pointers
Stack
Recursion

Given the head of a singly linked list, decide whether its values read the same from front to back as from back to front.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

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

  1. Set slow and fast to head, and while fast and fast.next exist, move slow one step and fast two steps.
  2. When the loop stops, slow is at the start of the second half (or the exact middle for odd lengths).
  3. Reverse the list starting at slow with the usual prev, slow, tmp pointer flip, so prev ends as the head of the reversed half.
  4. Walk left from head and right from prev together.
  5. If any pair of values differs, return false; when right runs out, return true.

Step-by-step example

head = [1,2,2,1]
  1. Both pointers start at the first 1. After one step, slow is at the first 2 and fast is at the second 2.
  2. fast.next is the last 1, so they move again: slow reaches the second 2 and fast becomes None. The second half starts at the second 2.
  3. Reversing 2, 1 gives 1, 2, so prev points at the last node, whose value is 1.
  4. Compare 1 with 1, then 2 with 2. Both match.
  5. right reaches 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 True

Time and space complexity

Time

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.

Space

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 left is not None instead of while right is not None. 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.next without checking fast first, which crashes on an empty list.
  • Losing the rest of the list during reversal by overwriting slow.next before saving it in tmp.
  • 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.