Function to check if a singly linked list is palindrome | Program to check if a linked list is palindrome ; Link list node ; Function to check if given linked list is palindrome or not ; To handle odd size list ; initialize result ; Get the middle of the list . Move slow_ptr by 1 and fast_ptrr by 2 , slow_ptr will have the middle node ; We need previous of the slow_ptr for linked lists with odd elements ; fast_ptr would become NULL when there are even elements in list . And not NULL for odd elements . We need to skip the middle node for odd case and store it somewhere so that we can restore the original list ; Now reverse the second half and compare it with first half ; NULL terminate first half ; Reverse the second half ; compare ; Reverse the second half again ; If there was a mid node ( odd size case ) which was not part of either first half or second half . ; Function to reverse the linked list Note that this function may change the head ; Function to check if two input lists have same data ; Both are empty reurn 1 ; Will reach here when one is NULL and other is not ; Push a node to linked list . Note that this function changes the head ; allocate node ; link the old list off the new node ; move the head to pochar to the new node ; A utility function to print a given linked list ; Drier program to test above function ; Start with the empty list
#include <stdbool.h> NEW_LINE #include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE struct Node { char data ; struct Node * next ; } ; void reverse ( struct Node * * ) ; bool compareLists ( struct Node * , struct Node * ) ; bool isPalindrome ( struct Node * head ) { struct Node * slow_ptr = head , * fast_ptr = head ; struct Node * second_half , * prev_of_slow_ptr = head ; struct Node * midnode = NULL ; bool res = true ; if ( head != NULL && head -> next != NULL ) { while ( fast_ptr != NULL && fast_ptr -> next != NULL ) { fast_ptr = fast_ptr -> next -> next ; prev_of_slow_ptr = slow_ptr ; slow_ptr = slow_ptr -> next ; } if ( fast_ptr != NULL ) { midnode = slow_ptr ; slow_ptr = slow_ptr -> next ; } second_half = slow_ptr ; prev_of_slow_ptr -> next = NULL ; reverse ( & second_half ) ; res = compareLists ( head , second_half ) ; reverse ( & second_half ) ; if ( midnode != NULL ) { prev_of_slow_ptr -> next = midnode ; midnode -> next = second_half ; } else prev_of_slow_ptr -> next = second_half ; } return res ; } void reverse ( struct Node * * head_ref ) { struct Node * prev = NULL ; struct Node * current = * head_ref ; struct Node * next ; while ( current != NULL ) { next = current -> next ; current -> next = prev ; prev = current ; current = next ; } * head_ref = prev ; } bool compareLists ( struct Node * head1 , struct Node * head2 ) { struct Node * temp1 = head1 ; struct Node * temp2 = head2 ; while ( temp1 && temp2 ) { if ( temp1 -> data == temp2 -> data ) { temp1 = temp1 -> next ; temp2 = temp2 -> next ; } else return 0 ; } if ( temp1 == NULL && temp2 == NULL ) return 1 ; return 0 ; } void push ( struct Node * * head_ref , char new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( struct Node * ptr ) { while ( ptr != NULL ) { printf ( " % c - > " , ptr -> data ) ; ptr = ptr -> next ; } printf ( " NULL STRNEWLINE " ) ; } int main ( ) { struct Node * head = NULL ; char str [ ] = " abacaba " ; int i ; for ( i = 0 ; str [ i ] != ' \0' ; i ++ ) { push ( & head , str [ i ] ) ; printList ( head ) ; isPalindrome ( head ) ? printf ( " Is ▁ Palindrome STRNEWLINE STRNEWLINE " ) : printf ( " Not ▁ Palindrome STRNEWLINE STRNEWLINE " ) ; } return 0 ; }