Description
Given the head of a singly linked list, return true if it is a palindrome or false otherwise. A linked list palindrome reads the same forwards and backwards when considering the values of the nodes.
Examples
Input:
head = [1,2,2,1]Output:
trueExplanation:
Reading node values forward: 1->2->2->1. Reading backward: 1->2->2->1. The sequence is identical, confirming it is a palindrome.
Input:
head = [1,2]Output:
falseExplanation:
Reading forward: 1->2. Reading backward: 2->1. Since 1 != 2 when comparing first and last, the sequence is not a palindrome.
Input:
head = [1]Output:
trueExplanation:
A single-element list has the same value when read forward or backward. Any single element is trivially a palindrome by definition.
Constraints
- •
The number of nodes is in the range [1, 10⁵]. - •
0 ≤ Node.val ≤ 9