PPractick

DSA interview question

Check Whether a Linked List Is a Palindrome

Medium · Data structures · Topics: linkedlist, two pointer

The problem

A DNA-sequence tool stores bases in a singly linked list and must check whether a sequence reads the same forwards and backwards. With an array you'd compare from both ends, but a singly linked list can't walk backwards. Copying the values to an array is O(n) extra space. The O(1)-space technique combines three earlier building blocks: find the middle with fast and slow pointers, reverse the second half in place, compare the two halves node by node, and then restore the list so the caller's data isn't left scrambled.

Example: 1 -> 2 -> 2 -> 1 → true; 1 -> 2 → false.

Function signature

bool isPalindrome(Node* head);  // O(1) extra space; the list is restored before returning

Test cases

Input listOutputExplanation
1 -> 2 -> 2 -> 1trueEven length
1 -> 2 -> 3 -> 2 -> 1trueOdd length: the middle is ignored
1 -> 2false
1 -> 2 -> 3false
1true
(empty)true

(Confirm: compare values; O(1) extra space is expected; the list must be unchanged after the check.)

How interviewers phrase it

Clarifying questions to ask

  1. May I modify the list temporarily? Must it be restored?
  2. Is O(1) extra space required?
  3. How long can the list be? (Recursion-based solutions use O(n) stack.)

Follow-ups to expect

  1. "What's the easy approach?"
  2. "How do you find where the second half starts?"
  3. "How is the odd-length middle handled?"
  4. "Why restore the list?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in