i cant figure out what’s wrong in my code.
i have created an another LL which is reverse of the actual LL.then i have compared both of them but it is giving wrong answer.
my code—>>>
Wrong output in palindrome of linked list
I have edited and checked your code… Pls try to submit now…
well all the code is submitted and all test cases are passed but if you will try to run the code by providing inputs it is not giving correct answer.
ex- giving true for 1 2 3 4 2 1
and mostly for all cases.
i cant figure out how all the test cases are passed
pls check @yuktimutreja01
my code is only comparing first and last node data.
Basically, The approach you are using is correct, as in your bool function, you have taken two head values, one for original linked list and other head when you have reversed the linked list,… and then you are comparing the values … if the value is not equal… you are returning false, else true…once you have traversed both linked list… For the input cases, in which your answer is not matching correctly… Try to use the approach I have explained here…
Other logic you could probably use is similar to wht you have used, but in different manner as,
where first you will determine the middle point in linked list, and from tht point… you will reverse and then check the data values again…
int palindrome(node *head)
{
int flag=1;
int count=getcount(head);
node *middle=findmidpoint(head);
middle=reverse(middle);
int step=0;
while(step<(count/2))
{
step++;
if(head->data!=middle->data)
{
flag=0;
}
else
{
head=head->next;
middle=middle->next;
}
}
return flag;
}
but why using two head values in bool function not giving correct answer.
The answer is correct for the test cases but for few cases, it would not give correct answer because of the approach…