PALINDROME LIST

Concepts Used

Linked list,recursion ,stack.

Difficulty Level

Easy

Problem Statement :

Check whether the given linked list is a palindrome or not.

Example:

2
3
2 5 2  true
5
4 5 6 3 4  false

First list is a palindrome since the first element is same as the last and middle one is common.
Second list is not a palindrome because the second element does not match the fourth element in the list.

See original problem statement here


EXPLANATION:

Approach 1(Linked list reversal):

Get the middle of the linked list.

Reverse the second half of the linked list.

Check if the first half and second half are identical.

Construct the original linked list by reversing the second half again and attaching it back to the first half.

Note: This approach takes O(n) time and O(1) extra space.


Approach 2(Using recursion):

Use two pointers left and right. Move right and left using recursion and check for following in each recursive call.Sub-list is palindrome.Value at current left and right are matching.
If both above conditions are true then return true.

    bool isPalindrome(Node*& left, Node* right)
    {
    // base case
    if (right == nullptr)
        return true;

    // return false on first mismatch
    if (!isPalindrome(left, right->next))
        return false;

    // copy left pointer
    Node* prevLeft = left;

    // advance the left pointer to the next node
    // this change would reflect in the parent recursive calls
    left = left->next;

    // In order for linked list to be palindrome, the character at the left
    // node should match with the character at the right node
    return (prevLeft->data == right->data);
    }

Approach 3(Using stack):

A simple solution is to use a stack of list nodes. This mainly involves three steps.Traverse the given list from head to tail and push every visited node to stack. Go thorugh the websites to learn coding and Traverse the list again. For every visited node, pop a node from stack and compare data of popped node with currently visited node.If all nodes matched, then return true, else false.

Note: This approach takes O(n) time and O(n) extra space(stack).


SOLUTIONS:

#include <stdio.h>  
    #include <stdbool.h>
    struct node{  
    int data;  
    struct node *next;  
    }; 
    struct node *head, *tail = NULL;  
    int size = 0;  
    void addNode(int data) {
    struct node *newNode = (struct node*)malloc(sizeof(struct node));  
    newNode->data = data;  
    newNode->next = NULL; 
    if(head == NULL) {  
        head = newNode;  
        tail = newNode;  
    }  
    else { 
        tail->next = newNode;
        tail = newNode;  
    } 
    size++;  
    } 
    struct node* reverseList(struct node *temp){  
    struct node *current = temp;  
    struct node *prevNode = NULL, *nextNode = NULL;
    while(current != NULL){  
        nextNode = current->next;  
        current->next = prevNode;  
        prevNode = current;  
        current = nextNode;  
    }  
    return prevNode;  
    } 
    void isPalindrome(){  
    struct node *current = head;  
    bool flag = true;
    int mid = (size%2 == 0)? (size/2)-1 : ((size)/2); 
    for(int i=1; i<mid; i++){  
        current = current->next;  
    } 
    struct node *revHead = reverseList(current->next);   
    while(head != NULL && revHead != NULL){  
        if(head->data != revHead->data){  
            flag = false;  
            break;  
        }  
        head = head->next;  
        revHead = revHead->next;  
    }  

    if(flag)  
        printf("true\n");  
    else  
        printf("false\n");  
    }  

    int main()  
    {  
    //Add nodes to the list
    int t;
    scanf("%d",&t);
    while(t--)
    {
      int n;scanf("%d",&n);

      for(int i=0;i<n;i++)
      {
        int x;scanf("%d",&x);
        addNode(x);  
      }size=n;
      isPalindrome();
    }
    return 0;  
    }  
#include<bits/stdc++.h>
    using namespace std;
    struct node{  
    int data;  
    struct node *next;  
    }; 
    struct node *head, *tail = NULL;  
    int size = 0;  
    void addNode(int data) {
    node *newNode = new node();  
    newNode->data = data;  
    newNode->next = NULL; 
    if(head == NULL) {  
        head = newNode;  
        tail = newNode;  
    }  
    else { 
        tail->next = newNode;
        tail = newNode;  
    } 
    size++;  
    } 
    struct node* reverseList(struct node *temp){  
    struct node *current = temp;  
    struct node *prevNode = NULL, *nextNode = NULL;
    while(current != NULL){  
        nextNode = current->next;  
        current->next = prevNode;  
        prevNode = current;  
        current = nextNode;  
    }  
    return prevNode;  
     } 
    void isPalindrome(){  
    struct node *current = head;  
    bool flag = true;
    int mid = (size%2 == 0)? (size/2)-1 : ((size)/2); 
    for(int i=1; i<mid; i++){  
        current = current->next;  
    } 
    struct node *revHead = reverseList(current->next);   
    while(head != NULL && revHead != NULL){  
        if(head->data != revHead->data){  
            flag = false;  
            break;  
        }  
        head = head->next;  
        revHead = revHead->next;  
    }  

    if(flag)  
        printf("true\n");  
    else  
        printf("false\n");  
    }  

    int main()  
    {  
    //Add nodes to the list
    int t;
    cin>>t;
    while(t--)
    {
      int n;cin>>n;

      for(int i=0;i<n;i++)
      {
        int x;cin>>x;
        addNode(x);  
      }size=n;
      isPalindrome();
    }
    return 0;  
    }  
 
import java.io.*;
    import java.util.*;
    public class Main{
    static class SinglyLinkedListNode {
        public int data;
        public SinglyLinkedListNode next;
        public SinglyLinkedListNode(int nodeData) {
            this.data = nodeData;
            this.next = null;
        }
    }
    static class SinglyLinkedList {
        public SinglyLinkedListNode head;
        public SinglyLinkedListNode tail;
        public SinglyLinkedList() {
            this.head = null;
            this.tail = null;
        }
        public void insertNode(int nodeData) {
            SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);

            if (this.head == null) {
                this.head = node;
            } else {
                this.tail.next = node;
            }

            this.tail = node;
        }
    }

    static boolean palindromeLlist(SinglyLinkedListNode head) {

        SinglyLinkedListNode slow = head;
        boolean ispalin = true;
        Stack<Integer> stack = new Stack<Integer>();

        while (slow != null) {
            stack.push(slow.data);
            slow = slow.next;
        }

        while (head != null) {

            int i = stack.pop();
            if (head.data == i) {
                ispalin = true;
            }
            else {
                ispalin = false;
                break;
            }
            head = head.next;
        }
        return ispalin;
    }

    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        int testCases = scanner.nextInt();
        while (testCases-- > 0) {
            SinglyLinkedList llist = new SinglyLinkedList();
            int llistCount = scanner.nextInt();
            for (int i = 0; i < llistCount; i++) {
                int llistItem = scanner.nextInt();
                llist.insertNode(llistItem);
            }
            boolean res =palindromeLlist(llist.head);
            if(res)
                System.out.println("true");
            else
                System.out.println("false");
        }
        scanner.close();
    }
    }

Previous post INSERT A NODE
Next post DELETE NODES FROM LINKED LIST

Leave a Reply

Your email address will not be published. Required fields are marked *