Check a Linked List Palindrome Without Mutating It
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Check whether a singly linked list is a palindrome in O(n) time and O(1) pointer space without leaving it modified. Practice finding the midpoint, reversing the second half, comparing values, and restoring every link before return.
Read the full Microsoft Software Engineer interview experience this question came from
Constraints
- 0 <= values.length <= 100.
- Each node value is an integer from -1,000,000,000 through 1,000,000,000.
- The implementation must restore the exact original linked-list structure before returning.
Examples
Input: [1, 2, 1]
Expected Output: True
Input: [1, 2]
Expected Output: False
Hints
- Find the midpoint with slow and fast pointers, then reverse only the second half.
- Save the reversed-half head and reverse that half again after comparison, even when a mismatch is found.