Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false.
METHOD 1 (Use a Stack)
- A simple solution is to use a stack of list nodes. This mainly involves three steps.
- Traverse the given list from head to tail and push every visited node to stack.
- Traverse the list again. For every visited node, pop a node from stack and compare data of popped node with currently visited node.
- If all nodes matched, then return true, else false.
Below image is a dry run of the above approach:
Below is the implementation of the above approach :
The time complexity of the above method is O(n).
METHOD 2 (By reversing the list)
This method takes O(n) time and O(1) extra space.
1) Get the middle of the linked list.
2) Reverse the second half of the linked list.
3) Check if the first half and second half are identical.
4) Construct the original linked list by reversing the second half again and attaching it back to the first half
To divide the list in two halves, method 2 of this post is used.
When number of nodes are even, the first and second half contain exactly half nodes. The challenging thing in this method is to handle the case when number of nodes are odd. We don’t want the middle node as part of any of the lists as we are going to compare them for equality. For odd case, we use a separate variable ‘midnode’.
a->NULL Palindrome b->a->NULL Not Palindrome a->b->a->NULL Is Palindrome c->a->b->a->NULL Not Palindrome a->c->a->b->a->NULL Not Palindrome b->a->c->a->b->a->NULL Not Palindrome a->b->a->c->a->b->a->NULL Is Palindrome
Time Complexity: O(n)
Auxiliary Space: O(1)
METHOD 3 (Using Recursion)
Use two pointers left and right. Move right and left using recursion and check for following in each recursive call.
1) Sub-list is palindrome.
2) Value at current left and right are matching.
If both above conditions are true then return true.
The idea is to use function call stack as container. Recursively traverse till the end of list. When we return from last NULL, we will be at last node. The last node to be compared with first node of list.
In order to access first node of list, we need list head to be available in the last call of recursion. Hence we pass head also to the recursive function. If they both match we need to compare (2, n-2) nodes. Again when recursion falls back to (n-2)nd node, we need reference to 2nd node from head. We advance the head pointer in previous call, to refer to next node in the list.
However, the trick in identifying double pointer. Passing single pointer is as good as pass-by-value, and we will pass the same pointer again and again. We need to pass the address of head pointer for reflecting the changes in parent recursive calls.
Thanks to Sharad Chandra for suggesting this approach.
a->NULL Not Palindrome b->a->NULL Not Palindrome a->b->a->NULL Is Palindrome c->a->b->a->NULL Not Palindrome a->c->a->b->a->NULL Not Palindrome b->a->c->a->b->a->NULL Not Palindrome a->b->a->c->a->b->a->NULL Is Palindrome
Time Complexity: O(n)
Auxiliary Space: O(n) if Function Call Stack size is considered, otherwise O(1).
- Check linked list with a loop is palindrome or not
- Check if a linked list of strings forms a palindrome
- Check if a doubly linked list of characters is palindrome or not
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- QuickSort on Singly Linked List
- Sum of the nodes of a Singly Linked List
- Binary Search on Singly Linked List
- Product of the nodes of a Singly Linked List
- Implement a stack using singly linked list
- Insertion Sort for Singly Linked List
- Convert a Singly Linked List to an array
- Alternate Odd and Even Nodes in a Singly Linked List
- Alternating split of a given Singly Linked List | Set 1