Code Logo

Linked List Palindrome

Published at25 Jul 2026
Singly Linked List Easy 0 views
Like0

Given an array representing a linked list, determine whether it is a palindrome — meaning it reads the same forward and backward. Return true if it is a palindrome, false otherwise.

For example, [1, 2, 2, 1] is a palindrome. [1, 2] is not. An empty array or single-element array is always a palindrome. [1, 2, 3, 2, 1] is also a palindrome. Negative values and duplicates are handled normally — only the sequence matters.

Checking whether a linked list is a palindrome is a classic interview problem. The optimal approach combines three techniques: finding the middle (slow/fast pointer), reversing the second half, and comparing both halves. This runs in O(n) time with O(1) extra space.

In the array version, you can simply use two pointers starting at both ends, comparing inward until they meet. This is equivalent to the linked list approach but simpler because arrays support direct index access. The linked list version requires the middle-finding and reversal steps because you cannot traverse backward.

Edge cases include an empty array (true), a single element (true), even-length palindromes like [1, 1] (true), and arrays where only the first and last match [1, 2, 3] (false).

Example Input & Output

Example 1
Input
[1]
Output
true
Explanation

Single element

Example 2
Input
[1,2,3,2,1]
Output
true
Explanation

Odd length palindrome

Example 3
Input
[]
Output
true
Explanation

Empty is palindrome

Example 4
Input
[1,2,2,1]
Output
true
Explanation

Palindrome

Example 5
Input
[1,2]
Output
false
Explanation

Not palindrome

Algorithm Flow

Recommendation Algorithm Flow for Linked List Palindrome
Recommendation Algorithm Flow for Linked List Palindrome

Solution Approach

Use two pointers starting at both ends, comparing elements as they move inward.

function solution(arr) {
  for (var i = 0, j = arr.length - 1; i < j; i++, j--) {
    if (arr[i] !== arr[j]) return false;
  }
  return true;
}

Initialize i at the start (0) and j at the end (length - 1). While i < j, compare arr[i] and arr[j]. If they differ, it is not a palindrome — return false. If they match, advance i forward and j backward. If the loop completes without finding a mismatch, the array is a palindrome — return true.

For the linked list version: find the middle using slow/fast pointers, reverse the second half, then compare the first and reversed second half element by element.

Time complexity is O(n), space complexity is O(1).

Best Answers

java
class Solution {
    public boolean solution(int[] nums) {
        for(int i=0,j=nums.length-1;i<j;i++,j--){if(nums[i]!=nums[j])return false;}return true;
    }
}