-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
2-6-palindrome.cpp
253 lines (213 loc) · 5.87 KB
/
2-6-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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Cracking the coding interview edition 6
* Implement a function to check if a list is a palindrome.
*
* Approach 1: Reverse the half the list and compare with other half.
* Approach 2: Iterative Approach
* - Push half the list in stack,
* - Compare the rest of the list by popping off from the stack
* Approach 3: Recursive Approach
*/
#include <iostream>
#include <stack>
struct Node {
char data;
Node * next;
Node ( char c ) : data{ c }, next{ nullptr } { }
};
/**
* [insert helper routine to insert new node at head]
* @param head [current head of the list]
* @param c [new node's data]
*/
void insert( Node * & head, char c ) {
Node * newNode = new Node(c);
newNode->next = head;
head = newNode;
}
/**
* [printList = helper routine to print the list]
* @param head [head of the list]
*/
void printList( Node * head ) {
while( head ) {
std::cout << head->data << "-->";
head = head->next;
}
std::cout << "nullptr" << std::endl;
}
/**
* [reversedList helper routine to reverse a list]
* @param head [head of current list]
* @return [reversed list's head]
*/
void reverse( Node * & head ) {
if ( head == nullptr || (head && (head->next == nullptr))){
return;
}
Node * newHead = nullptr;
Node * nextNode = nullptr;
while ( head ) {
nextNode = head->next;
head->next = newHead;
newHead = head;
head = nextNode;
}
head = newHead;
}
/**
* [isPallindromeIter1 - Iteratively determine if list is palindrome using reversing the list]
* @param head [Head node of the list]
* @return [True if list is palindrome, false if not]
*/
bool isPalindromeIter1( Node * head ) {
// if list is empty or just contains one node.
if ( head == nullptr || head->next == nullptr ) {
return true;
}
//step1 figure out middle node.
Node * ptr1 = head;
Node * ptr2 = head;
Node * middleNode = nullptr;
while( ptr2 && ptr1 && ptr1->next) {
ptr1 = ptr1->next->next;
ptr2 = ptr2->next;
}
//in case of odd number of nodes, skip the middle one
if ( ptr1 && ptr1->next == nullptr ) {
ptr2 = ptr2->next;
}
//reverse the second half of the list
reverse(ptr2);
middleNode = ptr2;
// now compare the two halves
ptr1 = head;
while( ptr1 && ptr2 && ptr1->data == ptr2->data ) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
//reverse the list again.
reverse(middleNode);
if ( ptr2 == nullptr ) {
return true;
} else {
return false;
}
}
/**
* [isPalindromeIter2 - Iteratively determine if list is palindrome using a stack]
* @param head [Head node of the list]
* @return [True if list is palindrome, false if not]
*/
bool isPalindromeIter2( Node * head ) {
// if list is empty or just contains one node.
if ( head == nullptr || head->next == nullptr ) {
return true;
}
Node * ptr1 = head;
Node * ptr2 = head;
//pushing the first half of list to stack.
std::stack<Node*> nodeStack;
while( ptr2 && ptr1 && ptr1->next ) {
ptr1 = ptr1->next->next;
nodeStack.push(ptr2);
ptr2 = ptr2->next;
}
//in case of odd number of nodes, skip the middle one
if ( ptr1 && ptr1->next == nullptr ) {
ptr2 = ptr2->next;
}
// Now compare the other half of the list with nodes
// we just pushed in stack
while(!nodeStack.empty() && ptr2) {
Node * curr = nodeStack.top();
nodeStack.pop();
if (curr->data != ptr2->data) {
return false;
}
ptr2 = ptr2->next;
}
return true;
}
/**
* [isPalindromeRecurHelper - Recursive approach to determine if list is palindrome]
* Idea is to use two pointers left and right, we move left and right to reduce
* problem size in each recursive call, for a list to be palindrome, we need these two
* conditions to be true in each recursive call.
* a. Data of left and right should match.
* b. Remaining Sub-list is palindrome.
* We are using function call stack for right to reach at last node and then compare
* it with first node (which is left).
* @param left [left pointer of sublist]
* @param right [right pointer of sublist]
* @return [true if sublist is palindrome, false if not]
*/
bool isPalindromeRecurHelper( Node * & left, Node * right ) {
//base case Stop when right becomes nullptr
if ( right == nullptr ) {
return true;
}
//rest of the list should be palindrome
bool isPalindrome = isPalindromeRecurHelper(left, right->next);
if (!isPalindrome) {
return false;
}
// check values at current node.
isPalindrome = ( left->data == right->data );
// move left to next for next call.
left = left->next;
return isPalindrome;
}
bool isPalindromeRecur( Node * head ) {
return isPalindromeRecurHelper(head, head);
}
int main()
{
Node * head1 = nullptr;
insert( head1, 'a' );
insert( head1, 'b' );
insert( head1, 'c' );
insert( head1, 'c' );
insert( head1, 'b' );
insert( head1, 'a' );
std::cout << "List 1: ";
printList( head1 );
if ( isPalindromeRecur( head1 ) ) {
std::cout << "List 1 is pallindrome list\n";
} else {
std::cout << "List 1 is not a pallindrome list\n";
}
std::cout << "List 1: ";
printList( head1 );
Node * head2 = nullptr;
insert( head2, 'r');
insert( head2, 'a');
insert( head2, 'd');
insert( head2, 'a');
insert( head2, 'r');
std::cout << "List 2: ";
printList( head2 );
if ( isPalindromeRecur( head2 ) ) {
std::cout << "List 2 is pallindrome list\n";
} else {
std::cout << "List 2 is not a pallindrome list\n";
}
std::cout << "List 2: ";
printList( head2 );
Node * head = nullptr;
insert( head, 'a' );
insert( head, 'b' );
insert( head, 'c' );
insert( head, 'b' );
insert( head, 'd' );
std::cout << "List 3: ";
printList( head );
if ( isPalindromeRecur( head ) ) {
std::cout << "List 3 is pallindrome list\n";
} else {
std::cout << "List 3 is not a pallindrome list\n";
}
std::cout << "List 3: ";
printList( head );
return 0;
}