The “Mirror” Interview Challenge
Imagine you are in a technical interview. The interviewer asks: “How do you check if a Singly Linked List reads the same forwards and backwards?”
Your first instinct might be to copy the list into an array and use two pointers. Stop right there. While that works, an interviewer at a top-tier company is looking for Space Optimization. They want to see if you can solve it with O(1) Auxiliary Space—meaning you don’t create any extra copies of the data.
In this article, we will solve LeetCode 234 using the “Split, Reverse, and Compare” strategy. This is a classic “hard-easy” problem that tests your comfort with pointer manipulation.
This problem is frequently asked in:
- Product-based companies (Amazon, Microsoft, Google)
- DSA rounds for freshers and experienced developers
- Coding platforms like LeetCode, GeeksforGeeks
Problem Statement (LeetCode 234)
Given the head of a singly linked list, return True if it is a palindrome, otherwise return False.
Example :
Input: 1 → 2 → 2 → 1
Output: True
Input: 1 → 2 → 3 → 4
Output: False
⚠️ Why This Problem is Tricky
In arrays, checking a palindrome is easy:
👉 Compare start and end using indexes
But in a singly linked list:
- No backward traversal ❌
- No random access ❌
So you need a smart workaround
The Interview Strategy: “Divide and Conquer”
To check for a palindrome without extra memory, we follow a three-step professional workflow:
-
The Midpoint Hunt: Use a Slow and Fast Pointer (Hare and Tortoise) to find the exact middle of the list.
-
The Tail Flip: Reverse the second half of the linked list in-place.
-
The Symmetry Check: Compare the original head with the new tail.
Interview Keywords to Remember: Two-Pointer Technique, In-Place Reversal, Floyd’s Cycle-Finding Logic, Space Complexity, Time-Space Tradeoff.
Python Code Implementation (Optimal Approach)
This solution is designed to pass all LeetCode test cases with maximum efficiency.
def isPalindrome(head):
if head is None or head.next is None:
return True
# Step 1: Find middle (Slow & Fast pointer)
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# Step 2: Reverse second half
prev = None
curr = slow
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
# Step 3: Compare both halves
first = head
second = prev
while second:
if first.val != second.val:
return False
first = first.next
second = second.next
return True
Detailed Tabular Dry Run
Let’s analyze a “Non-Palindrome” case to see where the logic catches the error.
Input List: [1] -> [2] -> [3] -> [1]
Phase 1: Finding Middle
slow stops at 3, fast hits None.
Phase 2: Reversing Second Half
Original second half: 3 -> 1
Reversed second half: 1 -> 3
Phase 3: The Comparison
| Comparison Step | Left Pointer Value | Right Pointer Value | Result |
| Iteration 1 | 1 | 1 | Match |
| Iteration 2 | 2 | 3 | MISMATCH! |
Final Result: The function returns False.
Why Interviewers Love This Question
-
Multiple Concepts in One: You aren’t just solving a palindrome; you are demonstrating that you can find a midpoint and reverse a list—two other common interview questions!
-
Pointer Precision: It tests if you can handle
None(null) values without the code crashing (AttributeErrors). -
Efficiency: It shows you prioritize Time Complexity O(n) and Space Complexity O(1).
Key Takeaways for Students
-
Don’t be wasteful: Arrays are easy, but pointers are efficient.
-
Slow/Fast is the key: This “Gap” logic we learned in the “Nth Node from End” article is the foundation for almost all Linked List problems.
-
Visualizing is winning: If you get stuck in an interview, draw the nodes and the
prev,curr, andnextpointers on the whiteboard.
Still confused? Watch the full explanation :
Get the Full Practice Set
I have uploaded the complete practice script to GitHub. It includes a helper function to create a linked list from an array so you can test it with different values of N.
[Access the Practice Code on GitHub]
Build a Real Project (Don’t Stop at Theory!)
If you really want to master Linked Lists, don’t just solve problems — build something real.
I’ve created a complete DSA Project – Expense Tracker using only:
- Singly Linked List
- Basic operations (insert, delete, reverse, traversal)
[Click here to explore the full project]
This will help you:
- Understand real-world use cases
- Strengthen DSA concepts
- Prepare for interviews + projects