xxxxxxxxxx
The idea is to use a stack and push all the nodes into the stack,
then again iterate over the linked list to validate
if the linked list is palindrome or not.
by comparing stack-pop against each iteration of linked list
data elements.
xxxxxxxxxx
// Include header file
#include <stdio.h>
#include <stdlib.h>
// C program for
// Check if palindrome exists in doubly linked list
// This is Linked List Node
typedef struct LinkNode
{
// Define useful field of LinkNode
int data;
struct LinkNode * next;
struct LinkNode * prev;
}LinkNode;
LinkNode * getLinkNode(int data)
{
// Create dynamic memory of LinkNode
LinkNode * ref = (LinkNode * ) malloc(sizeof(LinkNode));
if (ref == NULL)
{
// Failed to create memory
return NULL;
}
ref->data = data;
ref->next = NULL;
ref->prev = NULL;
return ref;
}
typedef struct DoublyLinkedList
{
// Define useful field of DoublyLinkedList
struct LinkNode * head;
struct LinkNode * tail;
}DoublyLinkedList;
DoublyLinkedList * getDoublyLinkedList()
{
// Create dynamic memory of DoublyLinkedList
DoublyLinkedList * ref =
(DoublyLinkedList * ) malloc(sizeof(DoublyLinkedList));
if (ref == NULL)
{
// Failed to create memory
return NULL;
}
// Set head and tail
ref->head = NULL;
ref->tail = NULL;
return ref;
}
// Insert new node at end position
void insert(DoublyLinkedList * ref, int value)
{
// Create a node
LinkNode * node = getLinkNode(value);
if (ref->head == NULL)
{
// Add first node
ref->head = node;
ref->tail = node;
return;
}
// Add node at last position
ref->tail->next = node;
node->prev = ref->tail;
ref->tail = node;
}
// Display node element of doubly linked list
void display(DoublyLinkedList * ref)
{
if (ref->head == NULL)
{
printf("Empty Linked List");
}
else
{
printf("Linked List Head to Tail :");
// Get first node of linked list
LinkNode * temp = ref->head;
// iterate linked list
while (temp != NULL)
{
// Display node value
printf(" %d ", temp->data);
// Visit to next node
temp = temp->next;
}
printf("\nLinked List Tail to Head :");
// Get last node of linked list
temp = ref->tail;
// iterate linked list
while (temp != NULL)
{
// Display node value
printf(" %d ", temp->data);
// Visit to prev node
temp = temp->prev;
}
}
}
int isPalindrome(DoublyLinkedList * ref)
{
if (ref->head == NULL)
{
// When empty linked list
return 0;
}
LinkNode * first = ref->head;
LinkNode * last = ref->tail;
while (last != NULL && first != NULL && first != last)
{
if (first->data != last->data)
{
// When node value are not same
return 0;
}
if (first->next == last)
{
// When get last pair
if (first->data == last->data)
{
// When pair value is same
return 1;
}
// When last middle node not same
return 0;
}
// Visit to next node
first = first->next;
// Visit to previous node
last = last->prev;
}
return 1;
}
int main()
{
DoublyLinkedList * dll = getDoublyLinkedList();
// Insert following linked list nodes
insert(dll, 1);
insert(dll, 2);
insert(dll, 4);
insert(dll, 3);
insert(dll, 4);
insert(dll, 2);
insert(dll, 1);
// Display all node
display(dll);
// Test A
if (isPalindrome(dll) == 1)
{
printf("\nYes\n");
}
else
{
printf("\nNo\n");
}
// Add new element
insert(dll, 1);
display(dll);
// Test B
if (isPalindrome(dll) == 1)
{
printf("\nYes\n");
}
else
{
printf("\nNo\n");
}
}
xxxxxxxxxx
// Java program for the above approach
public class LinkedList{
// Head of the list
Node head;
Node left;
public class Node
{
public char data;
public Node next;
// Linked list node
public Node(char d)
{
data = d;
next = null;
}
}
// Initial parameters to this function are
// &head and head
boolean isPalindromeUtil(Node right)
{
left = head;
// Stop recursion when right becomes null
if (right == null)
return true;
// If sub-list is not palindrome then no need to
// check for the current left and right, return
// false
boolean isp = isPalindromeUtil(right.next);
if (isp == false)
return false;
// Check values at current left and right
boolean isp1 = (right.data == left.data);
left = left.next;
// Move left to next node;
return isp1;
}
// A wrapper over isPalindrome(Node head)
boolean isPalindrome(Node head)
{
boolean result = isPalindromeUtil(head);
return result;
}
// Push a node to linked list. Note that
// this function changes the head
public void push(char new_data)
{
// Allocate the node and put in the data
Node new_node = new Node(new_data);
// Link the old list off the the new one
new_node.next = head;
// Move the head to point to new node
head = new_node;
}
// A utility function to print a
// given linked list
void printList(Node ptr)
{
while (ptr != null)
{
System.out.print(ptr.data + "->");
ptr = ptr.next;
}
System.out.println("Null");
}
// Driver Code
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
char[] str = { 'a', 'b', 'a', 'c', 'a', 'b', 'a' };
for(int i = 0; i < 7; i++)
{
llist.push(str[i]);
llist.printList(llist.head);
if (llist.isPalindrome(llist.head))
{
System.out.println("Is Palindrome");
System.out.println("");
}
else
{
System.out.println("Not Palindrome");
System.out.println("");
}
}
}
}
// This code is contributed by abhinavjain194
xxxxxxxxxx
#include<bits/stdc++.h>
using namespace std;
// Declaration of a single Node
class Node {
public:
int data;
Node(int d){
data = d;
}
Node *ptr;
};
// Function that returns boolean value
bool isPalin(Node* head){
// Temp pointer
Node* slow= head;
// Create a stack
stack <int> s;
// First traversal to push all the elements to stack
while(slow != NULL){
s.push(slow->data);
slow = slow->ptr;
}
// Second Traversal to compare the stack and node
while(head != NULL ){
int i=s.top();
s.pop();
// Compare data
if(head -> data != i){
return false;
}
head=head->ptr;
}
return true;
}
// Driver Function
int main(){
// Create nodes
Node one = Node(31);
Node two = Node(32);
Node three = Node(33);
Node four = Node(34);
Node five = Node(35);
// Connect all the nodes
five.ptr = NULL;
one.ptr = &two;
two.ptr = &three;
three.ptr = &four;
four.ptr = &five;
Node* temp = &one;
// Call function to return bool if the list is palindrome or not
int result = isPalin(&one);
if(result == 1)
cout<<"The value is True\n";
else
cout<<"The value is False\n";
return 0;
}