Palindrome Linked List

Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

Follow up: Could you do it in O(n) time and O(1) space?

Example 1
[1] -> [2] -> [2] -> [1] -> null
Inputhead = [1,2,2,1]
Outputtrue
The list reads the same forward and backward, so it is a palindrome.
Example 2
[1] -> [2] -> null
Inputhead = [1,2]
Outputfalse
The list does not read the same forward and backward, so it is not a palindrome.

Constraints

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

Asked at 16 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate