-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome_linked_list.cpp
More file actions
108 lines (84 loc) · 1.59 KB
/
palindrome_linked_list.cpp
File metadata and controls
108 lines (84 loc) · 1.59 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <vector>
using namespace std;
class node
{
public:
int data;
node *next;
node(int data)
{
this->data = data;
this->next = NULL;
}
};
void insertAtTail(node *&head, int data)
{
if (head == NULL)
{
node *temp = new node(data);
head = temp;
}
node *toInsert = new node(data);
toInsert->next = NULL;
node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = toInsert;
}
void print(node *&head)
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
bool check_palindrom(vector<int>& arr){
int s = 0;
int e = arr.size()-1;
while(s<=e){
if(arr[s] != arr[e]){
return 0;
break;
}
s++;
e--;
}
return 1;
}
int main()
{
node *head = new node(4);
cout<<head->data<<endl;
cout<<head->next<<endl;
insertAtTail(head, 5);
insertAtTail(head, 6);
insertAtTail(head, 7);
insertAtTail(head, 7);
insertAtTail(head, 6);
insertAtTail(head, 5);
insertAtTail(head, 4);
print(head);
vector<int> arr;
node* temp = head;
while(temp != NULL){
arr.push_back(temp->data);
temp = temp->next;
}
int ans = check_palindrom(arr);
if(ans == 1){
cout<<"This is palindrome"<<endl;
}
else{
cout<<"Not palindrome"<<endl;
}
return 0;
}