Palindrome Linked List - Problem

Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

A palindrome is a sequence that reads the same forward and backward.

Example: The linked list 1 → 2 → 2 → 1 is a palindrome because it reads the same forwards and backwards.

Input & Output

Example 1 — Basic Palindrome
$ Input: head = [1,2,2,1]
Output: true
💡 Note: The linked list reads the same forwards (1→2→2→1) and backwards (1→2→2→1), so it's a palindrome.
Example 2 — Not a Palindrome
$ Input: head = [1,2]
Output: false
💡 Note: The linked list reads 1→2 forwards but 2→1 backwards, which are different, so it's not a palindrome.
Example 3 — Single Node
$ Input: head = [1]
Output: true
💡 Note: A single node is always a palindrome since it reads the same forwards and backwards.

Constraints

  • The number of nodes in the list is in the range [1, 105]
  • 0 ≤ Node.val ≤ 9

Visualization

Tap to expand
Palindrome Linked List: Check if reads same forwards and backwardsInput: Linked List [1,2,2,1]1221Forward: 1 → 2 → 2 → 1Backward: 1 → 2 → 2 → 1✓ Same in both directions!trueOutput: true (it's a palindrome)
Understanding the Visualization
1
Input
Linked list: 1→2→2→1
2
Check
Compare forwards vs backwards reading
3
Output
Return true if palindrome
Key Takeaway
🎯 Key Insight: Find the middle, reverse second half, then compare both halves for optimal O(1) space solution
Asked in
Facebook 45 Amazon 38 Microsoft 32 Google 28
78.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen