Practice palindrome checking through a singly linked-list interface. Handle odd and even lengths, account for changes to links, and consider the linear-time constant-space follow-up.
# Check Whether a Linked List Is a Palindrome
Given the `head` of a singly linked list, return whether its node values read the same from the beginning and the end.
### Function
`bool isPalindrome(ListNode* head)`
Each existing `ListNode` contains an integer `val` and a pointer `next`. The function receives the list itself, not an array wrapper.
### Example 1
```text
Input: head = [4,7,7,4]
Output: true
```
The outer values are both `4`, and the inner values are both `7`.
### Example 2
```text
Input: head = [3,1,3,2]
Output: false
```
The first and last values differ.
### Constraints
- The list contains between `1` and `100000` nodes.
- `0 <= Node.val <= 9`.
### Clarifications
Arrays in the examples describe values encountered by following `next` pointers. A single-node list is a palindrome. Return a boolean; do not return a reordered list or newly allocated substitute nodes.
### What a Strong Answer Covers
- Correct comparisons for both odd and even list lengths.
- The behavior for a single node and an early mismatch.
- Auxiliary storage and any changes the implementation makes to input links.
### Follow-up Questions
Can you solve the task in `O(n)` time using `O(1)` auxiliary space?
Overview: Practice palindrome checking through a singly linked-list interface. Handle odd and even lengths, account for changes to links, and consider the linear-time constant-space follow-up.
Given the head of a singly linked list, return whether its node values read the same from the beginning and the end.
Function
bool isPalindrome(ListNode* head)
Each existing ListNode contains an integer val and a pointer next. The function receives the list itself, not an array wrapper.
Example 1
Input: head = [4,7,7,4]
Output: true
The outer values are both 4, and the inner values are both 7.
Example 2
Input: head = [3,1,3,2]
Output: false
The first and last values differ.
Constraints
The list contains between
1
and
100000
nodes.
0 <= Node.val <= 9
.
Clarifications
Arrays in the examples describe values encountered by following next pointers. A single-node list is a palindrome. Return a boolean; do not return a reordered list or newly allocated substitute nodes.
What a Strong Answer Covers Guidance
Correct comparisons for both odd and even list lengths.
The behavior for a single node and an early mismatch.
Auxiliary storage and any changes the implementation makes to input links.
Follow-up Questions Guidance
Can you solve the task in O(n) time using O(1) auxiliary space?