Singly Linked List Symmetry Check (Non-Destructive)
Linked ListsAuto-graded · 20 test casesLast 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:
- O(n) time. The list is visited at most a constant number of times end to end.
- O(1) auxiliary space. No copying values into an array or a stack to achieve the backward comparison.
- Non-destructive. You may temporarily rewire
nextpointers 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:
- Locate the midpoint using slow/fast pointers (
fastadvances two nodes per step,slowadvances one). Whenfastcan no longer move forward,slowmarks the end of the first half. - Reverse the second half in place starting from
slow.next. - 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.
- Restore the original structure by reversing the second half again and reattaching it to
slow.next. - 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core idea
The problem reduces to checking whether the sequence of node values is a palindrome while respecting three constraints: linear time, constant auxiliary space, and full structural restoration. The judged function receives the list as a Python array, so a two-pointer scan on that array is both correct and sufficient. The pointer-level algorithm described below is what the problem is really designed to exercise in an interview setting — both approaches produce identical results.
Two-pointer scan on the array (judged version)
Place one index at the start (left = 0) and one at the end (right = len(nodes) - 1). Advance them toward each other, comparing values at each step. If any pair differs, the list is not symmetric. If the indices meet or cross without a mismatch, it is symmetric.
Early return is safe here because the input array is never modified — there is nothing to restore.
Pointer-level algorithm (interview version)
Step 1 — Find the midpoint. Move slow one step and fast two steps per iteration, stopping when fast.next or fast.next.next is None. This places slow at the last node of the first half regardless of whether the total length is odd or even.
- Odd length (e.g.,
1→2→3→2→1):slowlands on3, the center node. The second half to reverse is2→1. - Even length (e.g.,
1→2→2→1):slowlands on the first2. The second half to reverse is2→1.
In both cases the second half is no longer than the first, so the comparison loop (while p2:) terminates correctly and the unpaired center element in odd-length lists is never examined.
Step 2 — Reverse the second half in place. Starting from slow.next, reverse all next pointers iteratively using three variables (prev, curr, nxt). This is O(n/2) time and O(1) space.
Step 3 — Compare without returning early. Walk p1 from the head and p2 from the new head of the reversed second half simultaneously. Store the boolean result in a variable. On a mismatch, set the flag and break — do not return yet.
Step 4 — Restore. Reverse the second half again and reattach it to slow.next. Every next pointer is now back to its original value.
Step 5 — Return the saved flag.
Why restoration matters
Returning immediately on a mismatch leaves the second half reversed. Any code that traverses the list afterward — including the caller and concurrent readers — will see a corrupted or truncated structure. The contract the problem enforces is that the function must be a pure query: the observable list structure is unchanged after the call returns.
Correctness
After reversing the second half, its i-th node holds values[n-1-i]. Comparing it with the i-th node from the head (values[i]) for every i < ⌊n/2⌋ is exactly the definition of a palindrome. Reversing a singly linked list twice returns it to its original state, and reattaching via slow.next repairs the one severed link.
Complexity
- Time: O(n). Each of the four passes (midpoint scan, first reversal, comparison, second reversal) visits at most n/2 nodes.
- Space: O(1) auxiliary. Only a fixed number of pointers or integer indices are used, regardless of list length.
Edge cases
- Empty list:
leftstarts at 0,rightat -1, so the loop body never executes — returnsTrue. - Single node:
left == rightimmediately — returnsTrue. - Two nodes: one comparison, correct for both
[4, 4]and[4, 5]. - Mismatch on the very first pair: exercises the restore-before-return path in the pointer version.
Reference solution
from typing import List
def is_symmetric_list(nodes: List[int]) -> bool:
left, right = 0, len(nodes) - 1
while left < right:
if nodes[left] != nodes[right]:
return False
left += 1
right -= 1
return True