Palindrome Linked List
Determine if a singly linked list is a palindrome in O(n) time and O(1) space by reversing the second half in place.
01The problem
Given the head of a singly linked list, return true if it is a palindrome or false otherwise.
Can you do it in O(n) time and O(1) extra space?
| Input | Output | Why |
|---|---|---|
[1, 2, 2, 1] | true | Reads 1, 2, 2, 1 forwards and backwards |
[1, 2] | false | First element (1) differs from last element (2) |
[1, 2, 1] | true | Odd length palindrome with center at 2 |
[1] | true | Single node list is trivially a palindrome |
Constraints: 1 to 100,000 nodes, node values between 0 and 9.
Loading the rest of the lesson…