Given the head of a singly linked list, return true if it is a palindrome, or false otherwise.
head = [1,2,2,1]truehead = [1,2]falseslow. prev ends up as the new right-half head.left from head, right from prev. Any mismatch → return false.A palindrome reads the same forwards and backwards. By reversing the second half in-place we avoid using a stack (O(n) space). The slow/fast pointer trick finds the midpoint in a single O(n) pass. The final comparison is also O(n). Total: O(n) time, O(1) space.
Copying the values into an array and checking with two pointers uses O(n) space. We cannot walk a singly linked list backwards.
Find the middle with fast and slow pointers. Reverse the second half. Then compare the first half with the reversed second half, node by node.
Middle, reverse, compare. Three small linked list tools combine into one solution.
Run the animation above on your own input, or watch the 1:03 video explanation of Palindrome Linked List with a dry run and Java code.