Skip to content

Commit 05e9cf7

Browse files
authored
Create is_palindrome.cpp
1 parent 41a0dbc commit 05e9cf7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
bool isPalindrome(Node *head)
2+
{
3+
Node *slow=head,*fast=head,*prev=NULL,*nxt=NULL;
4+
bool ans=true;
5+
if(!head)
6+
return !ans;
7+
while(fast->next and fast->next->next)
8+
{
9+
fast=fast->next->next;
10+
nxt=slow->next; // reversing the first half link list
11+
slow->next=prev;
12+
prev=slow;
13+
slow=nxt;
14+
}
15+
Node *left,*right;
16+
if(fast->next==NULL)
17+
{
18+
right=slow->next;
19+
left=prev;
20+
}
21+
else
22+
{
23+
right=slow->next;
24+
slow->next=prev;
25+
left=slow;
26+
}
27+
28+
while(left and right)
29+
{
30+
if(left->data!=right->data)
31+
return 0;
32+
left=left->next;
33+
right=right->next;
34+
}
35+
return 1;
36+
}

0 commit comments

Comments
 (0)