Linked List Palindrome

Given the head of a linked list, determine if it is a palindrome (reads the same forward and backward). Use O(n) time and O(1) extra space. Example: Input: head = [1,2,2,1] Output: true Explanation: The list forwards/backwards are both [1,2,2,1]. Pattern focus: Fast and Slow Pointers (find middle, reverse second half).

Input Format

head = ListNode

Output Format

return boolean

Constraints

  • The number of nodes is in the range [1, 10^5].
  • 0 <= Node.val <= 9

Examples

Example 1:

Input:

head = [1,2,2,1]

Output:

true

Explanation:

Sequence is same forwards/backwards.

Example 2:

Input:

head = [1,2]

Output:

false

Explanation:

Forwards [1,2] ≠ backwards [2,1].

Loading...
Linked List Palindrome - Linked List DSA Problem