Example 1
Input
head = [1,2,2,1]Output
TrueCheck whether the values in a singly linked list read the same forward and backward. Return True if they do, otherwise False.
head = [1,2,2,1]TrueCompare node values, not node identities. Both solutions below preserve the original list. A single node is a palindrome.
LeetCode 234 ↗head = [1,2,2,1] result = True
Visit N nodes, reverse N values, and compare at most N positions. These linear passes add up to O(N).
The collected values and their reversed copy require O(N) extra space.
head = [1,2,2,1] result = True
Both approaches take linear time. Reversing the second half avoids an extra values list, reducing extra space from O(N) to O(1).
Finding the midpoint, two reversals, and comparisons do O(N) total work.
A fixed number of references is enough. The reverse helper is iterative, so it does not use a growing recursion stack.