All problems

Palindrome Linked List

Easy · linked list

You are given the head of a singly linked list of numbers. Return true if the values read the same from the first node to the last as they do from the last node to the first, and false otherwise.

A list with a single node reads the same both ways, so it counts as a palindrome. Only the values matter, not the nodes themselves.

The nodes you receive are ListNode objects with a val and a next. You may re-point them while you work if you wish.

Examples

Example 1

Input: head = [3, 8, 8, 3]

Output: true

Read forwards 3, 8, 8, 3 and backwards 3, 8, 8, 3: identical.

Example 2

Input: head = [2, 5, 7, 5, 4]

Output: false

The first value is 2 but the last is 4, so the two readings differ.

Example 3

Input: head = [6]

Output: true

Constraints