Check if Linked List is a Palindrome

Data Structures
Medium
Cisco
99K views

Given the head of a singly linked list, return true if it is a palindrome. Solve in $O(n)$ time and $O(1)$ space by reversing the second half.

Why Interviewers Ask This

Cisco evaluates this question to assess a candidate's ability to optimize space complexity while manipulating linear data structures. They specifically look for mastery of pointer manipulation, the capacity to modify lists in-place without using auxiliary storage like arrays or stacks, and the logical rigor required to handle edge cases such as single nodes or even-length lists.

How to Answer This Question

1. Clarify constraints immediately by confirming the list is singly linked and that O(1) space is mandatory, ruling out stack-based recursion or array conversion. 2. Propose the standard three-phase algorithm: locate the middle using slow and fast pointers, reverse the second half of the list in-place, and compare nodes from both ends moving toward the center. 3. Walk through a concrete example, such as [1, 2, 2, 1], verbally tracing how the fast pointer finds the midpoint and how the reversal logic swaps pointers. 4. Discuss edge cases explicitly, including empty lists, single-node lists, and two-node lists where the logic must remain robust. 5. Conclude by emphasizing that while the list is modified during comparison, you would restore it to its original state before returning, demonstrating respect for side effects.

Key Points to Cover

  • Explicitly mention the slow and fast pointer strategy for finding the middle
  • Demonstrate clear understanding of in-place reversal mechanics
  • Address O(1) space complexity as a non-negotiable requirement
  • Discuss restoring the list to its original state after comparison
  • Identify specific edge cases like odd versus even length lists

Sample Answer

To solve this efficiently within Cisco's strict performance requirements, I would avoid converting the list to an array or using recursion due to the O(n) space constraint. Instead, I will implement a three-step approach. First, I'll use the slow and fast pointer technique to find the exact middle of the linked list. The slow pointer moves one step at a time while the fast pointer moves two; when fast reaches the end, slow is at the center. Second, I will reverse the second half of the list starting from the node after the middle. This requires careful pointer manipulation to ensure we don't lose the connection to the first half. Third, I will iterate with two pointers—one starting at the head and the other at the new start of the reversed second half—comparing values until the end of the reversed section. If all values match, it is a palindrome. For example, in a list [1, 2, 3, 2, 1], the middle is 3. Reversing the second half gives us [1, 2, 3, 2, 1] effectively becoming [1, 2, 3, 2, 1] visually during comparison. Finally, to be professional, I would reverse the second half back to its original order before returning true, ensuring no side effects on the input data structure.

Common Mistakes to Avoid

  • Using a stack or array to store values, which violates the O(1) space constraint
  • Failing to handle the odd-length list case correctly during the split point
  • Modifying the list permanently without restoring it, causing unintended side effects
  • Not checking for null or single-node inputs before attempting pointer operations

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 27 Cisco questions