// compute the length of linked list int n = 0; ListNode *curr = head; while(curr) { curr = curr->next; n++; } if (n <= 1) returntrue;
// move curr to the second half of linked list curr = head; for (int i = 0; i < n / 2; i++) { curr = curr->next; } if (n % 2) { curr = curr->next; }
// reverse the second half of linked list ListNode *prev = nullptr; ListNode *next = curr->next; curr->next = prev; while(next) { prev = curr; curr = next; next = curr->next; curr->next = prev; }
// compare the first half & reversed second half while(curr && head) { if (curr->val != head->val) returnfalse; curr = curr->next; head = head->next; }