Algorithm-Day24-Palindrome-Linked-List-lc-234
🧩 Problem Description
Given the head of a singly linked list, return true
if it is a palindrome, or false
otherwise.
Example:
1 | Input: 1 → 2 → 2 → 1 |
💡 Brute Force Idea
- Store values in a list or array.
- Check if the list is a palindrome.
1 | public boolean isPalindrome(ListNode head) { |
- Time: O(n)
- Space: O(n)
- ✅ Easy to implement, but not in-place.
💡 Optimal Approach (In-Place, O(1) Space)
✨ Strategy
- Find the middle of the list (fast and slow pointers).
- Reverse the second half.
- Compare the two halves.
- (Optional) Restore the list.
🔢 Java Code
1 | class Solution { |
⏱ Time and Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)
✍️ Summary
- Key techniques used:
- Fast/slow pointer
- In-place list reversal
- Two-pointer comparison
- This approach keeps space minimal while maintaining correctness.
This is a great example of combining several core linked list techniques in a single elegant solution.