-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromell.cpp
74 lines (73 loc) · 1.29 KB
/
palindromell.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
71
72
73
74
bool isPalindrome(Node *head)
{
int c=0,a;
Node* temp=head;
Node* temp1=NULL;
while(temp!=NULL)
{
c++;
temp=temp->next;
}
if(c%2!=0)
{
a=(c/2)+1;
temp1=head;
while(a)
{
temp1=temp1->next;
a--;
}
}
else
{
a=c/2;
temp1=head;
while(a)
{
temp1=temp1->next;
a--;
}
//cout<<temp1->data<<" ";
}
c=c/2;
Node* cur=head;
Node* prev=NULL;
Node* next=NULL;
while(c)
{
next=cur->next;
cur->next=prev;
prev=cur;
cur=next;
c--;
}
head=prev;
/*while(prev!=NULL)
{
cout<<prev->data<<" ";
prev=prev->next;
}
cout<<"\n";
while(temp1!=NULL)
{
cout<<temp1->data<<" ";
temp1=temp1->next;
}
cout<<"\n";*/
Node* temp2=temp1;
while(prev!=NULL && prev->data==temp1->data)
{
//cout<<prev->data<<" "<<temp1->data<<"\n";
prev=prev->next;
temp1=temp1->next;
}
if(prev!=NULL && prev->data!=temp1->data)
{
return false;
}
else
{
return true;
}
// return true;
}