-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromeLinkedList.cpp
78 lines (63 loc) · 1.42 KB
/
PalindromeLinkedList.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
75
76
77
78
//head is the head of you linked list
// Following is the node structure
/**************
class node{
public:
int data;
node * next;
node(int data){
this->data=data;
this->next=NULL;
}
};
***************/
int length(node* head){
int count=0 ;
node* temp = head ;
while(temp!=NULL){
temp=temp->next ;
count++ ;
}
return count ;
}
node* reverse(node* head)
{
if (head == NULL || head->next == NULL)
return head;
node* rest = reverse(head->next);
head->next->next = head;
/* tricky step -- see the diagram */
head->next = NULL;
/* fix the head pointer */
return rest;
}
bool check_palindrome(node* head)
{
//write your code here
//Figuring out the Mid Point of the Linked List :
int result = length(head);
int mid = result/2 ;
node* head1 = head ;
int i=0 ;
while(i<mid-1){
head1=head1->next ;
i++ ;
}
node* head2 = head ;
int g=0 ;
while(g<=mid-1){
head2=head2->next ;
g++ ;
}
head1->next=NULL ;
node* tail = reverse(head2);
head1=head ;
while(head1!=NULL || tail!=NULL){
if(head1->data!=tail->data){
return false ;
}
head1=head1->next ;
tail=tail->next ;
}
return true ;
}