educative.io

Palindrome Linked List

On line javascript solution 37, problem challenge 1 in Fast and slow pointers section ( Palindrome LinkedList), why are we checking if the head or the headSecondHalf is null again for us to decide if the linkedlist is a palindrome when we are doing it in this block of the while loop
while ((head !== null && headSecondHalf !== null)) {
if (head.value !== headSecondHalf.value) {
break; // not a palindrome
}

    head = head.next;
    headSecondHalf = headSecondHalf.next;
  }

We will reverse the second half once we have the middle of the LinkedList. Then, we will compare the first half with the reversed second half to see if the LinkedList represents a palindrome. While we compare them, we will do it while both lists go to the empty pointer. For this purpose, we check that both pointers have reached the endpoint.