I have a Single Linked List. I want to find that Linked List is Palindrome or not. I have implemented it in One way as below.
bool palindromeOrNot(node *head) {
node *tailPointer;
node *headLocal=head;
node *reverseList=reverseLinkedListIteratively(head);
int response=1;
while(headLocal != NULL && reverseList!=NULL) {
if(headLocal->id==reverseList->id) {
headLocal=headLocal->next;
reverseList=reverseList->next;
}
else
return false;
}
if(headLocal == NULL && reverseList==NULL)
return fasle;
else
return true;
}
I am reversing the original Linked List and then comparing Node by Node. If everything is fine then I will return 1 else return 0.
Is there a better algorithm to solve the problem.
How about having 2 pointers at the beginning of the link list.setting ptr1=k=start=1 and ptr2 =n where n is total number of nodes in linked list. We can then start with K and compare it with n-k+1 if both are same then increment k and go on comparing till we reach middle of the linked list
This logic (Using Recursive) will work if you have two string objects in LinkedList.
Just
reverse
half of the linked list. And start comparing. You don't need toreverse
the whole linked list.METHOD 1 (Use a Stack)
A simple solution is to use a stack of list nodes. This mainly involves three steps.
Time complexity of above method is O(n), but it requires O(n) extra space. Following methods solve this with constant extra space.
METHOD 2 (By reversing the list)
This method takes O(n) time and O(1) extra space.
METHOD 3 (Using Recursion)
Use two pointers left and right. Move right and left using recursion and check for following in each recursive call.
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.
More: geeksforgeeks
Whether a single-linked list is Palindrome or not
, can also be checkedwithout reversing the linked list
A recursive approach can be approached where a pointer pointing to the start of the linked list, & another pointer returning from the recursion from the last will be compared.
Here is the pseudo code:
Call is made like:
isPalindrome(&root,root);
I think the optimal solution would be to NOT using extra space, meaning NOT to use a new reversed LL... the idea is to take advantage of the operation stack that recursion uses... because when the recursion had reached the base case, it would start popping the stack from the last inserted node, which is the last node of the LL... I'm actually half way through this and hit a wall.. some how the root and the last node are offset... see my code
And the output looks like this:
If you can figure it out, please let me know too