I am trying to solve isPalindrome() question on LeetCode using non recurisve solution, When i run this code using VSCode it runs and gives me the right output, but when i run it in LeetCode compiler it gives me the error mentioned below.
Can you help me solve this problem ? and is there's any modifications on my code may help me solve the problem in a better way ?
bool isPalindrome(struct ListNode* head){
struct ListNode * NewHead, *MidList, *EndList;
NewHead = EndList = MidList = head;
struct ListNode *ptr_SecondHalf, *ptr_FirstHalf;
while (EndList->next != NULL)
{
EndList = EndList->next->next;
MidList = MidList->next;
if (EndList->next == NULL || EndList->next->next == NULL){
//ODD
if (EndList->next == NULL){
ptr_SecondHalf = MidList->next;
break;
}
//EVEN
if (EndList->next->next == NULL){
ptr_SecondHalf = MidList->next;
break;
}
}
}
MidList->next = NULL;
ptr_FirstHalf = head;
// Reverse SecondHalf
struct ListNode* ptr_SecondHalf_Reversed = NULL;
struct ListNode* current = ptr_SecondHalf;
struct ListNode* next = NULL;
while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = ptr_SecondHalf_Reversed;
// Move pointers one position ahead.
ptr_SecondHalf_Reversed = current;
current = next;
}
while (ptr_FirstHalf->next != NULL && ptr_SecondHalf_Reversed != NULL){
if (ptr_FirstHalf->val == ptr_SecondHalf_Reversed->val){
if (ptr_FirstHalf->next->next != NULL || ptr_SecondHalf_Reversed ->next != NULL){
ptr_FirstHalf = ptr_FirstHalf->next;
ptr_SecondHalf_Reversed = ptr_SecondHalf_Reversed ->next ;
}
else
{
break;
}
}else{
printf("false \n");
return false;
}
}
printf("true \n");
return true;
}
Line 12: Char 21: runtime error: member access within null pointer of type 'struct ListNode' [solution.c]