Thursday, August 4, 2011

In-Place and O(n) Algorithm for finding Palindrome in a SLL

struct list_node *pal; //GLOBAL VAR

void PalindromeCheck(struct list_node *node)
{
static int i=1;
static int mid;

i++;
if ( node->next == NULL )
{
pal=head->start;
if (node->data!=pal->data){
printf("\nNot Palindrome\n");
exit(0);
}
else {
i--;
}
mid=i/2;
return;
}
PalindromeCheck(node->next);
pal=pal->next;
i--;
if ((i >= mid) && (node->data!=pal->data)) {
printf("\nNot Palindrome\n");
exit(0);
}

if (i < mid) {
printf("\nPalindrome\n");
exit(0);
}
}