-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathDOUBLY_LL_PALINDROME.CPP
65 lines (54 loc) · 1.16 KB
/
DOUBLY_LL_PALINDROME.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
// Structure of node
struct Node
{
char data;
struct Node *next;
struct Node *prev;
};
/* Given a reference (pointer to pointer) to
the head of a list and an int, inserts a
new node on the front of the list. */
void push(struct Node** head_ref, char new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node ;
(*head_ref) = new_node;
}
// Function to check if list is palindrome or not
bool isPalindrome(struct Node *left)
{
if (left == NULL)
return true;
// Find rightmost node
struct Node *right = left;
while (right->next != NULL)
right = right->next;
while (left != right)
{
if (left->data != right->data)
return false;
left = left->next;
right = right->prev;
}
return true;
}
int main()
{
struct Node* head = NULL;
push(&head, 'l');
push(&head, 'e');
push(&head, 'v');
push(&head, 'e');
push(&head, 'l');
if (isPalindrome(head))
printf("It is Palindrome");
else
printf("Not Palindrome");
return 0;
}