Here's my code.
The concept is to traverse the whole linkedlist by taking two pointers fast and slow ,once slow is at the middle ,will reverse the list and compare it with the second half which is fast.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
ListNode *fast= head;
ListNode *slow= head;
while( fast!=NULL&&fast->next !=NULL)
{
fast=head->next->next;
slow=head->next;
}
slow= reverse(slow);
fast= head;
while(slow!=NULL)
{
if(fast->data!=slow->data)
return false
slow =slow->next;
fast=fast->next;
}
return true;
}
public: ListNode reverse(ListNode *head){
ListNode *prev = NULL;
while(head!=NULL)
{
ListNode* nextnode=head->next;
head->next=prev;
prev=head;
head=nextnode;
}
return prev;
}
};
I am getting this minor error,plz help to rectify the code
Line 22: Char 15: error: assigning to 'ListNode *' from incompatible type 'ListNode'
slow= reverse(slow);