Linked List Palindrome
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
Single element
Odd length palindrome
Empty is palindrome
Palindrome
Not palindrome
Algorithm Flow

Solution Approach
Use two pointers starting at both ends, comparing elements as they move inward.
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
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;
}
}Related Linked List Challenges
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
