Singly linked list palindrome

I have to check if a singly linked list forms a palindrome of characters. Each node keeps a character.
I can't use a double linked list. With the code I wrote I'm getting this on a few tests -1073741819 (0xC0000005)
I also get a warning : control reaches end of non-void function. I don't understand what I'm doing wrong...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  bool palindrome(node *head)
{   node*last=head->next->next;  //last gets this value so we can enter the 
                                    while loop
    node* first=head;

    while(first->next!=last && first!=last)
        { last=first;
            while(last->next)
             last=last->next;   //getting the last value in the list

            if(last->info!=first->info)
                return false;              //end function if the list is not a 
                                              palindrome
            else
            {                           
                node* first_del=first;   //if first and last are equal, I 
                first=first_del->next;  //delete both of them so I can check
                delete first_del;       //if the next elements in the list make 
                                        //up a palindrome

                node*del=last;
                last=NULL;
                delete del;

            }

          }
        if(first->next==last && first->next->info==last->info)
                return true;
                else if(first==last)
                       return true;
    }


//if all nodes to the middle of the list were deleted, and the value/s in the middle are still equal, the list is a palindrome.
Last edited on
control reaches end of non-void function
- a function which has a type (like int foo() ) did not contain a return statement or the return statement is inside a condition and the other path is missing one, etc.


being lazy, I have to note that:
-insert at head of a list is simple. It happens to reverse the input naturally.
- you already have the input string.
- if you were to, I dunno, iterate the list forwards comparing against the string's letters, it would tell you the answer using the above 2 bullets.
- you can also just insert 1/2 the chars then check what is left against what you put in, again using the insert at top reversal idea.

I don't see your bug.
Last edited on
You're deleting the nodes in the list as you go. Is it really okay for palindrome() to destroy the list that's passed into it?

Line 2: this will crash if the list has 0 or 1 items.
Line 23: you delete the last node, but the previous node still points to it.

I'm thinking that the algorithm is wrong here. I suspect that there's an easy way to do it with recursion.
I don't care about the list as I only have to make this function work, but I can see why it's a bad practice. The list must have at least 3 nodes so that's not a problem either.
What I thought I was doing at line 23 was that after I deleted the last node, the last but one would be the "new last" node and the function would keep going. I'm going to try recursion too. Thanks a lot!
Topic archived. No new replies allowed.