LeetCode 234: Palindrome Linked List (Java) | Intuition, Dry Run & O(1) Space Solution
LeetCode 234: Palindrome Linked List (Java) | Intuition, Dry Run & O(1) Space Solution

LeetCode 234: Palindrome Linked List (Java) | Intuition, Dry Run & O(1) Space Solution

Learn how to solve LeetCode 234 Palindrome Linked List using an optimal O(n) time and O(1) space approach. Includes intuition, dry run video, and clean Java solution.

11 views
0
0

🧩 Problem Overview

Given the head of a singly linked list, determine whether it is a palindrome.

A palindrome means the sequence reads the same forward and backward.

Example:

  1. Input: [1,2,2,1] → Output: true
  2. Input: [1,2] → Output: false

🎯 Why This Problem Matters

This problem tests:

  1. Linked list traversal
  2. Two-pointer technique (slow & fast)
  3. In-place reversal

It’s a must-know pattern for interviews.

🧠 Intuition

A simple approach is to copy elements into an array and check for palindrome.

But that uses extra space O(n).

Optimal Idea:

We can solve this in O(1) space by:

  1. Finding the middle of the linked list
  2. Reversing the second half
  3. Comparing both halves

🎥 Dry Run (Step-by-Step Explanation)

👉 Watch the complete dry run and pointer movement below:


This video explains how slow and fast pointers work and how the comparison is performed.

⚙️ Approach

Step 1: Handle Edge Cases

  1. If list has one node → return true

Step 2: Find Middle

  1. Use slow and fast pointers
  2. Fast moves 2 steps, slow moves 1

Step 3: Reverse Second Half

  1. Reverse the list starting from the middle

Step 4: Compare Both Halves

  1. Compare values from:
  2. start of list
  3. start of reversed half

💻 Java Solution

class Solution {
public ListNode reverse(ListNode head) {
ListNode curr = head;
ListNode prev = null;

while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}

return prev;
}

public boolean isPalindrome(ListNode head) {
if (head == null) return false;
if (head.next == null) return true;

ListNode slow = head;
ListNode fast = head;

// Find middle
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}

// Reverse second half
ListNode secondHalf = reverse(slow);

// Compare both halves
ListNode firstHalf = head;

while (secondHalf != null) {
if (secondHalf.val != firstHalf.val) {
return false;
}
secondHalf = secondHalf.next;
firstHalf = firstHalf.next;
}

return true;
}
}

🔍 Dry Run (Quick Summary)

Example:

1 → 2 → 2 → 1

  1. Middle found
  2. Second half reversed
  3. Compare values one by one

Result → Palindrome ✅

⏱️ Time and Space Complexity

  1. Time Complexity: O(n)
  2. Space Complexity: O(1)

⚠️ Edge Cases

  1. Single node
  2. Two nodes
  3. Odd length list
  4. Even length list

💡 Key Takeaways

  1. Fast & slow pointer technique is essential
  2. Reversing linked list is a reusable pattern
  3. Helps optimize space complexity

🚀 Final Thoughts

This is a classic interview problem combining multiple linked list techniques.

Make sure you understand:

  1. Pointer movement
  2. Reversal logic
  3. Comparison step

👉 For full clarity, don’t skip the video explanation above.

Ai Assistant Kas