Question: Write a program to check if the given Linked List is a palindrome or not?

    Input:
    1.> 4, 8, 15, 16, 23, 42
    2.> 4, 8, 15, 16, 15, 8, 4

    Output:
    1.> IS NOT A PALINDROME
    2.> IS A PALINDROME

    What is a Palindrome?

    According to Wikipedia, A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction.

    Thus, numbers like 121, 1331, 98711789 are palindromes.

    Similarly, Linked Lists can be palindromes when they remain the same when read from the forward direction or the backward direction.

    To check if a Linked List is a palindrome, we can perform the following operations on the Linked List.

    • Get the middle of the Linked List
    • Separate the two lists.
    • Reverse the second half of the Linked List.
    • Compare the first and the second half.
    • If both the halves are same, then the list is a palindrome.

    We have discussed, how to get the middle of a Linked List in this post.

    Once we get the middle of the Linked List we separate the lists. If the list is ODD, we ignore the center node, as it will node play a part in being a palindrome, 121, 14541, 198303891 are some examples and here 2, 5, 0 do not play any part in being a palindrome.

    In case of EVEN list, we get the n/2th node, and no change is needed. If list is 1, 2, 3, 3, 2, 1 we get the underlined 3.

    Once we get the middle node, we can reverse the List using the method in this post.

    Now we compare the Lists, till (count/2) iterations, where count = number of nodes. If at any point of time, the data is not equal, that means the List is NOT PALINDROME, else we can say that the list IS A PALINDROME.

    #include<stdio.h>
    
    struct node
    {
    	int data;
    	struct node * next;
    };
    
    struct node * findMiddleOfLinkedList(struct node * head)
    {
    	struct node * slowPtr = head;
    	struct node * fastPtr = head;
    
    	while(fastPtr ->next != NULL)
    	{
    		slowPtr = slowPtr -> next;
    		fastPtr = fastPtr -> next;
    		if(fastPtr == NULL)
    			break;
    
    		fastPtr = fastPtr -> next;
    		if(fastPtr == NULL)
    			break;
    	}
    	return slowPtr;
    }
    
    struct node * reverseList(struct node * head)
    {
        struct node * temp = NULL;
        struct node * nextNode = NULL;
        while(head)
        {
            nextNode = head -> next;
            head -> next = temp;
            temp = head;
            head = nextNode;
        }
        return temp;
    }
    
    //function to get the number of nodes
    int getCount(struct node * head)
    {
    	int count = 0;
    	while(head)
    	{
    		count++;
    		head = head -> next;
    	}
    	return count;
    }
    
    void printList(struct node * head)
    {
    	while(head != NULL)
    	{
    		printf("%d ",head->data);
    		head = head->next;
    	}
    }
    
    // defining a function to determine if a List is a Palindrome or not
    int isPalindrome(struct node * head)
    {
    	int flag = 1;
    
    	// get the count of the List
    	int count = getCount(head);
    
    	// variable to get the middle of the List
    	struct node * middle = findMiddleOfLinkedList(head);
    
    	// reverse the list from the middle
    	middle = reverseList(middle);
    
    	// now "middle" point to the MID of the Linked List
    	// "head" points to the start of the Linked List
    
    	// a variable to count the number of iterations
    	int step = 0;
    
    	printf("count = %d\n",count);
    
    	// if ODD example = 1335331, count = 7, mid = 5, we need to go till (7/2 = 3)
    	// if EVEN example = 123321, count = 6, mid - 3, we need to go till (6/3 = 3)
    	while(step < (count/2))
    	{
    		step++;
    
    		if(head -> data != middle -> data)
    		{
    			// if there is a mismatch at any point of time, set the flag to FALSE
    			flag = 0;
    		}
    
    		// advance both the pointers
    		head = head -> next;
    		middle = middle -> next;
    	}
    
    	return flag;
    }
    
    struct node * addElement(struct node *head, int number)
    {
    	struct node * temp = (struct node*)malloc(sizeof(struct node));
    	temp -> data = number;
    	temp -> next = NULL;
    	struct node * temp2 = head;
    	while(temp2 -> next != NULL)
    		temp2 = temp2 -> next;
    	temp2 -> next = temp;
    	return head;
    }
    
    int main(void)
    {
    	//creating a list
    	struct node * listHead = (struct node*)malloc(sizeof(struct node));
    	listHead->data = 1;
    	listHead->next = NULL;
    	listHead = addElement(listHead, 2);
    	listHead = addElement(listHead, 3);
    	listHead = addElement(listHead, 4);
    	listHead = addElement(listHead, 4);
    	listHead = addElement(listHead, 3);
    	listHead = addElement(listHead, 2);
    	listHead = addElement(listHead, 1);
    
    	if(isPalindrome(listHead))
    		printf("IS A PALINDROME");
    	else
    		printf("IS NOT A PALINDROME");
    
    	return 0;
    }
    

    NOTE: This method changes the original Linked List, in order to retain the original list, either make a copy of the original list and then perform the operation, or reverse the middle list once again and rejoin it to the original list.

    2 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a Linked List, we need to find the middle of the list and print the number. Input: 4, 8, 15, 16, 23, 42, 99 Output: 16 This is a very basic question and is a part of a bigger problem. However, there are several approaches to solve the above problem. BRUTE-FORCE APPROACH In this approach, we count the number of nodes in the list for each of the node and see whether it is the middle. struct node * bruteForce(struct node * head) { // making 2 temporary …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked ListMisc

    Reverse a Linked List in pairs.

    by nikoo28
    2 minutes read

    Question: Write a program to reverse a Linked List in pairs. That means we should reverse the 1st and 2nd numbers, 3rd and 4th numbers and so on. Input: 4, 8, 15, 16, 23, 42 Output: 8, 4, 16, 15, 42, 23 The basic idea behind solving this problem is that we need to traverse the entire list and we need to keep on swapping the values of 2 adjacent nodes. Since we need to reverse the values in pairs, it is also clear that we need to move 2 …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a Linked List, we need to find out if the number of nodes in the Link List are odd or even? Input: 4, 8, 15, 16, 23, 42 Output: EVEN The most easy method to solve this problem would be by traversing the entire Linked List and counting the number of nodes as we go. As the loop is finished, we can check if the count is even or odd. A Simpler Way: Another way of solving this problem in less time would be advancing 2 nodes at …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Print the Linked List in reverse order. The actual structure of the Linked List must remain intact. Input: 4, 8, 15, 16, 23, 42 Output: 42, 23, 16, 15, 8, 4 We just discussed how to reverse a Linked List in an older post. Reverse a singly Linked List. But, in the above described method we changed the entire structure of the Linked List, the last node was no longer the last node and the head node had also changed. We might encounter cases, where we just need to …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked List

    Reverse a singly Linked List.

    by nikoo28
    3 minutes read

    Question: How will you reverse a singly Linked List? Input:– 4 -> 8 -> 15 -> 16 -> 23 -> 42 Output:– 42 -> 23 -> 16 -> 15 -> 8 -> 4 Reversing a singly Linked List is based upon a very simple concept. We just need to swap the “NEXT” value of each of the node, as we iterate over the list. That means that after the complete process, the first node should point to NULL, and the second last node should point to the first node…and so …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: In C you can do something like:- char s[] = “Hello World”; and char *s = “Hello World”; What is the difference between the two of them? What happens to the memory during compile time and run-time? This declaration: char s[] = "hello"; Creates ONE object – a char array of size 6, called s, initialized with the values ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More