easy Problem 4 Find if Doubly Linked List is a Palindrome
Problem Statement
Given a doubly linked list, determine whether it is a palindrome.
A doubly linked list is a palindrome if it reads the same backward as forward, utilizing the previous and next pointers of the nodes.
Examples
-
Example 1:
- Input: 1 <-> 2 <-> 3 <-> 2 <-> 1
- Output: true
- Justification: The list reads the same backward as forward.
-
Example 2:
- Input: 1 <-> 2 <-> 2 <-> 3
- Output: false
- Justification: Reading backward, the list is 3 <-> 2 <-> 2 <-> 1, which is not the same as reading forward.
-
Example 3:
- Input: 1 <-> 1 <-> 1 <-> 1
- Output: true
- Justification: All elements are the same, so the list is a palindrome.
Try it yourself
Try solving this question here:
✅ Solution Find if Doubly Linked List is a Palindrome
Problem Statement
Given a doubly linked list, determine whether it is a palindrome.
A doubly linked list is a palindrome if it reads the same backward as forward, utilizing the previous and next pointers of the nodes.
Examples
-
Example 1:
- Input: 1 <-> 2 <-> 3 <-> 2 <-> 1
- Output: true
- Justification: The list reads the same backward as forward.
-
Example 2:
- Input: 1 <-> 2 <-> 2 <-> 3
- Output: false
- Justification: Reading backward, the list is 3 <-> 2 <-> 2 <-> 1, which is not the same as reading forward.
-
Example 3:
- Input: 1 <-> 1 <-> 1 <-> 1
- Output: true
- Justification: All elements are the same, so the list is a palindrome.
Solution
To determine whether a doubly linked list is a palindrome, we can utilize two pointers approach. Initialize two pointers, one at the start and one at the end of the doubly linked list. Compare the elements at these pointers. If the elements are equal, move the start pointer one step forward and the end pointer one step backward and compare again. If at any point the elements at the pointers are not equal, the list is not a palindrome. Continue this process until the start pointer is greater than or equal to the end pointer. If the loop finishes without finding unequal elements, the list is a palindrome.
Here is the step-by-step solution.
-
Check for Base Cases:
- If the head of the doubly linked list is
nullor if there is only one node (head's next isnull), returntrue.
- If the head of the doubly linked list is
-
Find the Tail Node:
- Start with the head node and traverse to the end of the list to find the tail node. This is achieved by iterating through the nodes using
nextpointers until you reach a node wherenextisnull.
- Start with the head node and traverse to the end of the list to find the tail node. This is achieved by iterating through the nodes using
-
Initialize Two Pointers:
- Two pointers are initialized:
startat the head of the list andendat the tail of the list.
- Two pointers are initialized:
-
Palindrome Check Loop:
- The list is traversed from both ends simultaneously towards the middle. In each iteration:
- Compare the values of the nodes pointed by
startandend. - If the values are not equal at any point, return
false, indicating that the list is not a palindrome. - Move
startforward (to the next node) andendbackward (to the previous node).
- Compare the values of the nodes pointed by
- The list is traversed from both ends simultaneously towards the middle. In each iteration:
-
Return Result:
- If the loop completes without returning
false, it means all mirrored elements in the list are equal. Hence, the function returnstrue, confirming that the list is a palindrome.
- If the loop completes without returning
This algorithm is effective because it leverages the nature of a doubly linked list, using both next and previous pointers to compare elements from both ends of the list, reducing the time complexity. As we are not using any additional data structures to store elements of the list and only using constant extra space, it is space-efficient as well.
Algorithm Walkthrough:
- Initialize two pointers:
startat the head of the list andendat the tail. - While the
startpointer is less than theendpointer:- Compare the values at the
startandendpointers. - If they are not equal, return false.
- Move the
startpointer one step forward and theendpointer one step backward.
- Compare the values at the
- If the loop finishes without returning, return true, as the list is a palindrome.
Here is the visual representation of the algorithm:
Code
Here is the code for this algorithm:
// class DLNode {
// int val;
// DLNode next, prev;
// DLNode(int val) { this.val = val; }
// }
class Solution {
public boolean isPalindrome(DLNode head) {
if (head == null || head.next == null) return true;
DLNode tail = head;
while (tail.next != null) tail = tail.next; // Find the tail of the doubly linked list
DLNode start = head;
DLNode end = tail;
while (start != end && start.prev != end) {
if (start.val != end.val) return false; // Compare the values at start and end pointers
start = start.next;
end = end.prev;
}
return true;
}
public static void main(String[] args) {
Solution solution = new Solution();
// Test case 1
DLNode head1 = new DLNode(1);
head1.next = new DLNode(2);
head1.next.prev = head1;
head1.next.next = new DLNode(1);
head1.next.next.prev = head1.next;
System.out.println(solution.isPalindrome(head1)); // Expected: true
}
}
Time Complexity
The algorithm traverses the doubly linked list twice: once to find the tail of the list and once to compare the elements for palindrome checking. Each traversal takes O(n) time, where n is the number of elements in the list.
-
Finding the Tail: We traverse the list from the head to the tail, which takes O(n) time.
-
Comparing Elements: We use two pointers, one starting from the head and the other from the tail, moving towards the center of the list. In the worst case, this also takes O(n) time.
Therefore, the total time complexity of the algorithm is O(n) + O(n) = O(2n), which is asymptotically equivalent to O(n).
Space Complexity
The algorithm uses only a constant amount of extra space to store the pointers (start, end, tail) and the input does not affect the space usage. Therefore, the space complexity of the algorithm is O(1), which means it uses constant space.
🤖 Don't fully get this? Learn it with Claude
Stuck on Problem 4 Find if Doubly Linked List is a Palindrome? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Problem 4 Find if Doubly Linked List is a Palindrome** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Problem 4 Find if Doubly Linked List is a Palindrome** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Problem 4 Find if Doubly Linked List is a Palindrome**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Problem 4 Find if Doubly Linked List is a Palindrome**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.