Back

Palindrome Linked List

easy

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

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

Test Cases

Copy an input into the main harness and Run to verify
Input
head = [1,2,2,1]
Expected Output
true
Input
head = [1,2]
Expected Output
false

Constraints

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

Hints

Hint 1 — click to reveal

Copying the values into an array makes this trivial — but that is O(n) space.

Hint 2 — click to reveal

Find the middle, reverse the second half, then compare the two halves.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.