Palindrome Linked List

EasyStringMathLinked ListGraph

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:true
Explanation:

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:false
Explanation:

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:true
Explanation:

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!