Community Problem

Palindrome Linked List

Difficulty: Easy

Decide whether a singly-linked list reads the same forward and backward in O(n) time and O(1) space using fast/slow pointers plus an in-place reverse of the second half.

Palindrome Linked List

Decide whether a singly-linked list reads the same forward and backward in O(n) time and O(1) space using fast/slow pointers plus an in-place reverse of the second half.

EASY
Free
linked-list
two-pointers

By CodeSnatch

February 21, 2026

·

Updated May 20, 2026

974 views

12

4.6 (16)

Picked this as the EASY-tier official Cat 5 entry because it's the cleanest place to teach the fast/slow + reverse-half pattern, which shows up again in Reorder List, Sort List, and Reverse Nodes in K-Group. The naive solution copies the values into an array and runs a two-pointer compare in O(n) extra space; the senior signal is the in-place version.

Palindrome Linked List

Given the head of a singly-linked list, return true if the list reads the same forward and backward, false otherwise. Each node stores an integer in its val field and a reference to the next node in next.

Examples

Example 1:

  • Input: head = [1, 2, 2, 1]
  • Output: true
  • Explanation: Reading forwards and backwards both give 1, 2, 2, 1.

Example 2:

  • Input: head = [1, 2]
  • Output: false
  • Explanation: Reverse is 2, 1.

Example 3:

  • Input: head = [1]
  • Output: true
  • Explanation: A single-node list is trivially a palindrome.

Example 4:

  • Input: head = [1, 2, 3, 2, 1]
  • Output: true
  • Explanation: Odd-length palindrome with a middle node.

Constraints

  • The number of nodes in the list is in the range [1, 10^5].
  • 0 <= Node.val <= 9

Follow-up

Can you do it in O(n) time and O(1) extra space? The interview answer is: walk fast/slow to find the midpoint, reverse the second half in place, then compare halves with two pointers from the outside in.

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems