π§© Problem Overview
Given the head of a singly linked list, determine whether it is a palindrome.
A palindrome means the sequence reads the same forward and backward.
Example:
- Input: [1,2,2,1] β Output: true
- Input: [1,2] β Output: false
π― Why This Problem Matters
This problem tests:
- Linked list traversal
- Two-pointer technique (slow & fast)
- In-place reversal
Itβs a must-know pattern for interviews.
π§ Intuition
A simple approach is to copy elements into an array and check for palindrome.
But that uses extra space O(n).
Optimal Idea:
We can solve this in O(1) space by:
- Finding the middle of the linked list
- Reversing the second half
- Comparing both halves
π₯ Dry Run (Step-by-Step Explanation)
π Watch the complete dry run and pointer movement below:
This video explains how slow and fast pointers work and how the comparison is performed.
βοΈ Approach
Step 1: Handle Edge Cases
- If list has one node β return true
Step 2: Find Middle
- Use slow and fast pointers
- Fast moves 2 steps, slow moves 1
Step 3: Reverse Second Half
- Reverse the list starting from the middle
Step 4: Compare Both Halves
- Compare values from:
- start of list
- start of reversed half
π» Java Solution
π Dry Run (Quick Summary)
Example:
1 β 2 β 2 β 1
- Middle found
- Second half reversed
- Compare values one by one
Result β Palindrome β
β±οΈ Time and Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)
β οΈ Edge Cases
- Single node
- Two nodes
- Odd length list
- Even length list
π‘ Key Takeaways
- Fast & slow pointer technique is essential
- Reversing linked list is a reusable pattern
- Helps optimize space complexity
π Final Thoughts
This is a classic interview problem combining multiple linked list techniques.
Make sure you understand:
- Pointer movement
- Reversal logic
- Comparison step
π For full clarity, donβt skip the video explanation above.





