Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false.

METHOD 1 (By reversing the list):
This method takes O(n) time and O(1) extra space.
1) Get the middle of the linked list.
2) Reverse the second half of the linked list.
3) Check if the first half and second half are identical.
4) Construct the original linked list by reversing the second half again and attaching it back to the first half
To divide the list into two halves, method 2 of this post is used.
When a number of nodes are even, the first and second half contain exactly half nodes. The challenging thing in this method is to handle the case when the number of nodes is odd. We don’t want the middle node as part of the lists as we are going to compare them for equality. For odd cases, we use a separate variable ‘midnode’.
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
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" );
}
int main()
{
struct Node* head = NULL;
char str[] = "abacaba" ;
int i;
for (i = 0; str[i] != '' ; i++)
{
push(&head, str[i]);
printList(head);
isPalindrome(head) ? printf ( "Is Palindrome" ) : printf ( "Not Palindrome" );
}
return 0;
}
|
Output:
a->NULL
Is Palindrome
b->a->NULL
Not Palindrome
a->b->a->NULL
Is Palindrome
c->a->b->a->NULL
Not Palindrome
a->c->a->b->a->NULL
Not Palindrome
b->a->c->a->b->a->NULL
Not Palindrome
a->b->a->c->a->b->a->NULL
Is Palindrome
Time Complexity: O(n)
Auxiliary Space: O(1)
METHOD 2 (Using Recursion):
Use two pointers left and right. Move right and left using recursion and check for following in each recursive call.
1) Sub-list is a palindrome.
2) Value at current left and right are matching.
If both above conditions are true then return true.
The idea is to use function call stack as a container. Recursively traverse till the end of the list. When we return from the last NULL, we will be at the last node. The last node is to be compared with the first node of the list.
In order to access the first node of the list, we need the list head to be available in the last call of recursion. Hence, we pass head also to the recursive function. If they both match we need to compare (2, n-2) nodes. Again when recursion falls back to (n-2)nd node, we need a reference to 2nd node from the head. We advance the head pointer in the previous call, to refer to the next node in the list.
However, the trick is identifying a double-pointer. Passing a single pointer is as good as pass-by-value, and we will pass the same pointer again and again. We need to pass the address of the head pointer for reflecting the changes in parent recursive calls.
Thanks to Sharad Chandra for suggesting this approach.
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct node
{
char data;
struct node* next;
};
bool isPalindromeUtil( struct node** left,
struct node* right)
{
if (right == NULL)
return true ;
bool isp = isPalindromeUtil(left,
right->next);
if (isp == false )
return false ;
bool isp1 = (right->data == (*left)->data);
*left = (*left)->next;
return isp1;
}
bool isPalindrome( struct node* head)
{
isPalindromeUtil(&head, head);
}
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\n" );
}
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\n\n" ) : printf ( "Not Palindrome\n\n" );
}
return 0;
}
|
Output:
a->NULL
Not Palindrome
b->a->NULL
Not Palindrome
a->b->a->NULL
Is Palindrome
c->a->b->a->NULL
Not Palindrome
a->c->a->b->a->NULL
Not Palindrome
b->a->c->a->b->a->NULL
Not Palindrome
a->b->a->c->a->b->a->NULL
Is Palindrome
Time Complexity: O(n)
Auxiliary Space: O(n) if Function Call Stack size is considered, otherwise O(1).
Please refer complete article on Function to check if a singly linked list is palindrome for more details!
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Mar, 2023
Like Article
Save Article