AlgoMaster Logo

Palindrome Linked List

Last Updated: March 29, 2026

easy
3 min read

Understanding the Problem

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.

Key Constraints:

  • 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 list has at least one node, so we don't need a null-check for an empty list.

Approach 1: Copy to Array

Intuition

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.

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 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?

Approach 2: Reverse Second Half (Optimal)

Intuition

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.

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