Solution Review: Problem Challenge 1
We'll cover the following
Palindrome LinkedList (medium) #
Given the head of a Singly LinkedList, write a method to check if the LinkedList is a palindrome or not.
Your algorithm should use constant space and the input LinkedList should be in the original form once the algorithm is finished. The algorithm should have time complexity where ‘N’ is the number of nodes in the LinkedList.
Example 1:
Input: 2 -> 4 -> 6 -> 4 -> 2 -> null
Output: true
Example 2:
Input: 2 -> 4 -> 6 -> 4 -> 2 -> 2 -> null
Output: false
Solution #
As we know, a palindrome LinkedList will have nodes values that read the same backward or forward. This means that if we divide the LinkedList into two halves, the node values of the first half in the forward direction should be similar to the node values of the second half in the backward direction. As we have been given a Singly LinkedList, we can’t move in the backward direction. To handle this, we will perform the following steps:
- We can use the Fast & Slow pointers method similar to Middle of the LinkedList to find the middle node of the LinkedList.
- Once we have the middle of the LinkedList, we will reverse the second half.
- Then, we will compare the first half with the reversed second half to see if the LinkedList represents a palindrome.
- Finally, we will reverse the second half of the LinkedList again to revert and bring the LinkedList back to its original form.
Code #
Here is what our algorithm will look like: