KatInterview

Singly Linked List Symmetry Check (Non-Destructive)

Linked ListsAuto-graded · 20 test cases
MediumAuto-gradedLinked Liststwo-pointerslinked-listsin-place

Last updated

Tests whether a candidate can verify palindrome structure on a singly linked list in O(n) time and O(1) space while restoring every pointer before returning.

Singly Linked List Symmetry Check (Non-Destructive)

A singly linked list is symmetric if its sequence of values reads identically from head to tail and from tail to head.

Implement is_symmetric_list(nodes) where nodes is a list of integers representing the node values of a singly linked list in traversal order (head first).

Return True if the list is symmetric, False otherwise.

Algorithm requirements

Your solution must meet all three of the following constraints, which reflect the actual pointer-level algorithm the problem is designed to exercise:

  1. O(n) time. The list is visited at most a constant number of times end to end.
  2. O(1) auxiliary space. No copying values into an array or a stack to achieve the backward comparison.
  3. Non-destructive. You may temporarily rewire next pointers during the check, but every pointer must be restored before the function returns — including when a mismatch is detected. A caller traversing the list afterward must observe exactly the original structure.

Algorithm sketch (pointer-level)

The intended approach for the linked-list version:

  1. Locate the midpoint using slow/fast pointers (fast advances two nodes per step, slow advances one). When fast can no longer move forward, slow marks the end of the first half.
  2. Reverse the second half in place starting from slow.next.
  3. Compare values from the head with values from the new head of the reversed second half. Record the result in a flag — do not return early on a mismatch.
  4. Restore the original structure by reversing the second half again and reattaching it to slow.next.
  5. Return the recorded result.

The comparison loop should be driven by the second half (while p2:), so the unpaired middle node in an odd-length list is never compared with anything and does not affect the result.

Early-return pitfall

Returning False the moment a mismatch is found — before the restore step — leaves the list permanently damaged. Always complete the restore step, then return the saved boolean.

Edge cases

  • An empty list is symmetric.
  • A single-node list is symmetric.
  • Negative values and duplicate values require no special handling beyond plain equality comparison.

Constraints

  • 0 <= len(nodes) <= 10^4
  • -10^5 <= nodes[i] <= 10^5

Examples

Example 1

Input: is_symmetric_list([])

Output: True

Example 2

Input: is_symmetric_list([1])

Output: True

Example 3

Input: is_symmetric_list([1, 2, 2, 1])

Output: True

Example 4

Input: is_symmetric_list([1, 2, 3, 2, 1])

Output: True

Hints

Hint 1

Use two indices (or slow/fast pointers) to close in from both ends — you never need to know the total length up front.

Hint 2

For the pointer-level version: once you find the midpoint, reverse the second half in place. Walk both halves simultaneously to compare, but store the result in a variable rather than returning immediately on a mismatch.

Hint 3

To restore the list, reverse the second half a second time and reattach it to the midpoint node. Only after that restoration should you return your saved boolean result.