Given a singly linked list, determine if it is a palindrome. Write a Boolean function to output true or false based on result
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
For a single linked we don't have a prev pointer to traverse back from the end of the list and to check with corresponding start positions, i.e., what we normally do to check a string to be palindrome. But for a single linked list we only can traverse forward.
Algorithm:
Attention here!!!
For any statement like, bool result=checkPalindrome(p->next) &(temp->data==p->data);
Where there is two expression linked with ‘&' or ‘&&' operator the first expression is evaluated first and in case if it's true, only the second expression is evaluated. If the first expression is false, second expression is not at all evaluated.
So for this particular statement,
checkPalindrome(p->next) is kept being evaluated until recursion reaches the base case & (temp->data==p->data) is not at all evaluated until the first expression results true .
Let's evaluate how the function works to check whether it's a palindrome.
For example 2:
C++ implementation
Output