forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindromic_list.cpp
70 lines (70 loc) · 1.21 KB
/
palindromic_list.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
66
67
68
69
70
/* Code to check if a linked list is palindromic or not using stacks
OBJECTIVE:
1. Push the data of each node into the stack(LIFO)
2. Pop each element of the stack and compare with the linked list element
*/
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next;
};
void push(Node **head, int new_data)
{
Node *temp = new Node();
temp->data = new_data;
temp->next = *head;
*head = temp;
}
int checkPalin(Node *head)
{
stack<int> s;
Node *temp = head;
while (temp != NULL)
{
s.push(temp->data);
temp = temp->next;
}
Node *ref = new Node();
ref = head;
while (ref != NULL)
{
int i = s.top();
s.pop();
if (i != ref->data)
{
return -1;
}
ref = ref->next;
}
return 1;
}
//Driver code
int main()
{
int n;
cout<<"Enter the length of the list";
cin>>n;
int a[n];
cin>>a[0];
Node *head = new Node();
head->data = a[0];
head->next = NULL;
for(int i=1;i<n;i++)
{
cin>>a[i];
push(&head,a[i]);
}
cout << checkPalin(head) << "\n";
}
/*
Time Complexity:O(n)
INPUT:
Enter the length of the list3
1
2
1
OUTPUT:
1
*/