All problemsBack
Palindrome Linked List
easyGiven 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 themain harness and Run to verifyInput
head = [1,2,2,1]Expected Output
trueInput
head = [1,2]Expected Output
falseConstraints
- 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.