Palindrome Linked List
Master this topic with zero to advance depth.
Palindrome Linked List
Given the head of a singly linked list, return true if it is a palindrome or false otherwise.
Visual Representation
List: 1 -> 2 -> 2 -> 1
1. Find mid: 2 (first 2)
2. Reverse second half: 1 -> 2
3. Compare halves: (1,2) == (1,2) -> trueExamples
Level I: Brute Force (Array Copy)
Intuition
Copy the values of the linked list into an array and then check if the array is a palindrome using two pointers.
Detailed Dry Run
1->2->2->1. Array: [1,2,2,1]. i=0, j=3: 1==1. i=1, j=2: 2==2. Match!
⚠️ Common Pitfalls & Tips
O(N) space is required.
Visual Trace (Array)
List: 1 -> 2 -> 3 -> 2 -> 1
Array: [1, 2, 3, 2, 1]
i=0, j=4: 1==1
i=1, j=3: 2==2
Match!Level II: Recursive / Stack-based
Intuition
Compare nodes from outside-in. A stack helps us access nodes in reverse order.
Visual Trace (Stack)
Stack: [1, 2, 2, 1]
Compare Pop(1) with Head(1) -> MatchLevel III: Optimal (Reverse Second Half)
Intuition
Find the middle of the linked list using fast/slow pointers. Reverse the second half of the list. Compare the two halves. Finally, restore the list (optional).
Detailed Dry Run
1->2->2->1.
- Mid: 2.
- Reverse second half: 1->2.
- Compare (1->2) and (1->2).
⚠️ Common Pitfalls & Tips
Be careful when the list length is odd; the slow pointer will be exactly at the middle node, and reversing from it works fine.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.