Skip to content
BytePatterns

Chain Reads the Same Backwards

EasyLinked Lists#fast-slow-pointers#in-place-reversal~20m

Problem

Given the head of a singly linked list of digits, return True if the values read the same from front to back as from back to front. The list has between 1 and 100,000 nodes. Aim for O(n) time and O(1) extra memory, which rules out copying the values into a Python list, and leave the list in its original shape when you are done.

Examples

Input:  1 -> 2 -> 2 -> 1
Output: True
Input:  1 -> 2
Output: False
Input:  7
Output: True
Why:    edge case, a single node is a palindrome

Hints

0 / 3

Stuck on the idea rather than the code? Fast and Slow Pointers covers it.