For the first question

*Q: Check whether given singly linked list is palindrome or not in
single pass.

Instead of making two passes, can we do it in single pass on a list?

One method i can think of is, hashing character to its postion and
check for the sum.*

I can think of a recursive solution. But recursion will use the extra space
on the stack. Note that here we are passing pointer p by reference and
pointer q by value. So when the value of p changes in one stack frame it
will be reflected in the previous frames as well.

bool isPalindrome(node*& p, node* q) {
   if (q->next == NULL) {
     return (p->data == q->data);
   } else {
     bool temp1 = isPalindrome(p, q->next);
     p = p->next;
     bool temp2 = (p->data == q->data);
     return (temp1 && temp2);
   }
}

Call this as isPalidrome(start, start);

On Tue, Sep 8, 2009 at 11:07 PM, ankur aggarwal <ankur.mast....@gmail.com>wrote:

> @bharath
> how will u recognize
> which "a" to compare to which "a"
>
> or apply on "malayalam"
>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to