← DiffPush

Check for palindrome LL

Standard Bar Linked List · Medium Problems of LL O(N) · O(1)

The Gate Reader That Must Not Duplicate Memory

A secure gate reads a one-way access chain and must confirm the code is a palindrome before opening — but the controller is too small to hold a second copy of the code. The trick: split the chain at its middle, flip the rear half in place, and then compare the two halves node by node as they march in lockstep.

Input: An array head of node values representing the linked list.

Output: true when the list reads the same forwards and backwards, false otherwise.

Constraints

Examples

Example 1

Input: {"head":[1,2,2,1]}
Output: true
Front half 1-2 matches the flipped rear half 2-1 station for station.

Example 2

Input: {"head":[1,2]}
Output: false
The halves disagree at the very first comparison.

Solve this in your browser →

Also on LeetCode ↗