last day (15 days later) » 

5:59 AM
-1
A: member access within null pointer of type, C programming is Palindrome

Devi Khositashviliyour solution idea isn't wrong, but is too hard to implement and don't have any bug, I suggest my solution or suggest to write one method which will return reverse list and then just compare these two. bool isPalindromeRec(struct ListNode* left, struct ListNode* right) { if(right == NULL) ret...

 
What is this printf(ans);?
This statement left = left->next; does not make a sense.
 
when recursion will backtrack left will be moved with one step and right will be moved back with one stap, so after right == NULL will return true, then it will continue to compare values of node[0] and node[n-1], node[1] = node[n-2] and so on...
 
left is a local variable of the function. changing it within a call of the function does not influence on the the variable used as an argument of the caller of the function.
 
and printf(ans); is true or false as question author wanted, you can check and output what you want, maim question was to find the answer, so I think your question is just to find some dirt in my code - please respect each other and don't try to just make someones answer downvote. thnx.
 
@DanielFarina He does not have iterations. He has recursive calls and is changing a local variable. Such changing does not influence on the other function calls.
 
5:59 AM
it's with reference so it will change, that's a reason why we need to make a copy at the start of the head, that's why we use NewHead and not head to don't lose the root
 
@DeviKhositashvili The first parameter of the function printf has the type const char * while you are passing an integer. As a result the function invokes undefined behavior.
@DanielFarina One more left is a local variable of the function. Changing it does not influence on the passed argument. This statement left = left->next; does not make a sense. It influences on nothing.
 
@VladfromMoscow, man its just a hint where the answer is saved and in case he wants to print he will use it, as I said - the main question was to find the answer, there is no need to care about it.. I'm not a compiler i just show him the way
in case if it works and prints it would be just 0 or 1, he wanted "true" or "false", so he can find out this easy task himself without worries... with simple if...
 
You wrote an invalid code printf(ans); that invokes undefined beahvior.
 
and? this code doesn't compile without the main or whole body of code anyway...
 
This code is not safe. The return statement invalid the assignment of left variable.
 
5:59 AM
@Devi Khositashvili It is the big difference when the code is not compiled due to the absence of other parts of the program or when the code contains invalid statements.
 
Thank you Devi for your solution, but as i mentioned i want to solve it in Non-Recursive way. Acutally i could solve it comparing the orginal Linkedlist and its revese using ReverseLinkedList function. As you can notice, i am trying different solution and you can notice that i already revesed the second part of the linked list only.
 
i can write another solution if you need
this should be useful too: geeksforgeeks.org/…
 
Thank you Devi, i mainly want to know the reason for the error i get, or a modification on the code i wrote, i checked out many solutions on the internet, i am trying to come up with my solution ( my way of learning), but i could not find an answer for the error.
 
@DeviKhositashvili Your recursive solution is totally wrong. For example for the first call of the function isPalindromeRec The second sub-expression in the if statement if(isPalindromeRec(left, right->next) && left->val == right->val) will in general evaluate always to false (with rare exceptions) because it compares values of the first and the second nodes.
@DeviKhositashvili Before suggesting a code try to compile it yourself. For example this declaration bool isPalindrome(ListNode head) has a typo.
 

last day (15 days later) »