AlgoMaster Logo

Palindrome Linked List

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

A palindrome reads the same forwards and backwards. With an array, you compare elements from both ends moving inward. A singly linked list only allows traversal in one direction, so there is no way to walk backwards from the tail.

The core challenge is comparing elements from the front and back of a structure that only supports forward traversal. Two approaches handle this: copy the values into an array that does support random access, or find the middle and reverse the second half in place to compare it against the first.

Key Constraints:

  • The number of nodes is in the range [1, 10^5]. With up to 100,000 nodes, any O(n^2) comparison would be too slow, so the target is O(n) time.
  • 0 <= Node.val <= 9. Values are single digits, so there is no overflow concern when comparing them.
  • The list has at least one node, so an empty-list check is unnecessary.

Approach 1: Copy to Array

Intuition

Copy all the node values into an array, then run the standard two-pointer palindrome check. An array supports random access, so comparing from both ends is direct.

This converts the linked list problem into an array problem, which sidesteps the lack of backward traversal at the cost of extra memory.

Algorithm

  1. Traverse the linked list and copy each node's value into an array.
  2. Use two pointers, left starting at index 0 and right starting at the last index.
  3. Compare values at left and right. If they differ, return false.
  4. Move left forward and right backward. Repeat until they meet or cross.
  5. If all comparisons matched, return true.

Example Walkthrough

1Initial linked list: 1 → 2 → 2 → 1
1
curr
2
2
1
null
1/2
1Copied values. Initialize left=0, right=3
0
left
1
1
2
2
2
3
right
1
1/4

Code

This uses O(n) extra space for the array. The next approach removes that array by rearranging the list itself.

Approach 2: Reverse Second Half (Optimal)

Intuition

If we find the middle of the linked list, we can reverse the second half in place and then compare both halves node by node, using no extra array.

To find the middle, advance a slow pointer one step and a fast pointer two steps at a time. When fast reaches the end, slow sits at the start of the second half. Reversing the list from slow onward lets us walk the first half forward and the reversed second half forward at the same time, comparing values as we go.

Algorithm

  1. Use slow/fast pointers to find the middle of the list. When fast reaches the end, slow points to the start of the second half.
  2. Reverse the second half of the list starting from slow.
  3. Compare nodes from the head of the original list and the head of the reversed second half.
  4. If any values differ, return false. If all match, return true.
  5. (Optional) Reverse the second half again to restore the original list.

Example Walkthrough

1Initialize: slow=node 0, fast=node 0
slow
1
fast
2
2
1
null
1/7

Code