Palindrome linked list without modifying the list
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's coding round, last asked Apr 2026. It tests linked lists and two pointers.
Decide whether a singly linked list reads the same forwards and backwards, in O(n) time and O(1) extra space. You may reverse part of the list while checking, but the caller must find every next pointer exactly as it was when the function returns.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
ListNodehasvalandnext. -
is_palindrome(head: ListNode | None) -> bool— leaves the list unchanged.
Lists below are written as their values in order.
Example
is_palindrome([1, 2, 2, 1]) -> True
is_palindrome([1, 2, 3, 2, 1]) -> True
is_palindrome([1, 2]) -> False
is_palindrome([]) -> True # empty and single-node lists are palindromes
After each call, walking the list from head gives the same nodes in the same order.
Constraints
- Up to 10^5 nodes; values are integers.
- O(1) extra space, not counting the input.
Clarifying questions
- Is an O(n) stack or array copy acceptable as a first version?
- Must the list be restored, or may it be left modified?
- Could other threads read the list while I check it?
What a strong answer covers
- The simple version first: copy values to an array (or push half on a stack) and compare; O(n) space.
- The O(1) version: find the middle with slow and fast pointers, reverse the second half, compare it against the first half, then reverse it back and reconnect.
- Restoring the list on every exit path, including an early mismatch: compare without returning, restore, then return.
- Odd versus even lengths, and the empty and one-node cases.
- Why the temporary mutation is unsafe if other threads read the list concurrently.
Follow-ups
- Do it recursively with O(n) stack instead; what is the trade-off?
- Check whether the list is a palindrome after removing at most one node.
- The list is doubly linked: what becomes trivial?
Where it's asked
- Microsoft · Coding · Last asked Apr 202660 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent
What it tests
linked lists · two pointers
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
