# 28ms C++ Solution with logic demonstration (Not the best but might help some people to understand)

• ``````class Solution
{
public:
{
int size = 0, counter = 0;
//get the size of the Linked list
while(count != NULL){
size+=1; count = count->next;
}
//if size is 1, it is Palindrome
if (size == 1)
return true;

//get the location for left_end pointer so that we can apply to the algorithm below
int locationForLeftEnd = size/2-1;

/*Logic Demonstration
=========================================================================
=========================================================================
[ 1 ] -----> [ 2 ] -----> [ 3 ] -----> [ 2 ] -----> [ 1 ] -----> [NULL]
=========================================================================*/
{
if(counter==locationForLeftEnd)

// depends on the size of the Linked list (odd/even), the location is different

counter+=1;
}

/*Logic Demonstration
=========================================================================
Step.2  Set left_end->next to NULL
=========================================================================
[ 1 ] -----> [ 2 ] -----> [NULL]       [ 2 ] -----> [ 1 ] -----> [NULL]
=========================================================================*/
left_end->next = NULL;

/*Logic Demonstration
=========================================================================
Step.3  Reverse the left side Linked list
=========================================================================
[ 2 ] -----> [ 1 ] -----> [NULL]       [ 2 ] -----> [ 1 ] -----> [NULL]
=========================================================================*/

/*Logic Demonstration
=========================================================================
Step.4  Compare two Linked lists and return true/false if they are the same
=========================================================================
[ 2 ] -----> [ 1 ] -----> [NULL]       [ 2 ] -----> [ 1 ] -----> [NULL]
=========================================================================*/
}

{
while(left != NULL && right != NULL)
{
if(left->val != right->val)
return false;
left = left->next;
right = right->next;
}
return true;
}

{
ListNode* prev = NULL;
while(node!=NULL)
{
ListNode* temp = node->next;
node->next = prev;
prev = node;
node = temp;
}
return prev;
}