-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.cpp
82 lines (71 loc) · 1.63 KB
/
palindrome.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
79
80
81
82
#include <iostream>
#include <unordered_map>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data = 0){
this->data = data;
this->next = NULL;
}
};
void print(Node* &head){
Node* temp = head;
while(temp){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
bool isPalindrome(Node* &head){
if(head == NULL || head->next == NULL)
return true;
// 1] find middle node
Node* slow = head, *fast = head->next;
while(fast){
fast = fast->next;
if(fast != nullptr){
fast = fast->next;
slow = slow->next;
}
}
// 2] reverse LL after slow th node
Node* p = NULL, *q = slow->next, *r = NULL;
while(q){
p = q->next;
q->next = r;
r = q;
q = p;
}
// Join both LL
slow->next = r;
// 3] Compare both LL
Node* temp1 = head, *temp2 = r;
while(temp2){
if(temp1->data != temp2->data)
return false;
temp1 = temp1->next;
temp2 = temp2->next;
}
return true;
}
int main()
{
Node* head = new Node(10);
Node* first = new Node(20);
Node* second = new Node(30);
Node* third = new Node(20);
Node* forth = new Node(10);
// Node* fifth = new Node(10);
head->next = first;
first->next = second;
second->next = third;
third->next = forth;
forth->next = NULL;
// fifth->next = NULL;
print(head);
cout<<isPalindrome(head)<<endl;
print(head);
return 0;
}