-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq2.6.cpp
More file actions
56 lines (45 loc) · 1.26 KB
/
q2.6.cpp
File metadata and controls
56 lines (45 loc) · 1.26 KB
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
#include "common_header.hpp"
#include <iostream>
#include <vector>
using namespace std;
bool check_palindrome(myspace::SingleLL *&ptr_start, myspace::SingleLL *ptr_end,
int &counter) {
bool palindrome_flag = true;
counter++;
if (ptr_end->next != NULL)
palindrome_flag = check_palindrome(ptr_start, ptr_end->next, counter);
// when last node in on the top of stack
else {
cout << "length of list: " << counter << '\n';
counter /= 2;
}
// common for all nodes
if ((palindrome_flag == false) || (counter == 0))
return palindrome_flag;
else {
counter--;
if (ptr_end->data == ptr_start->data) {
ptr_start = ptr_start->next;
return true;
} else
return false;
}
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
myspace::SingleLL *head;
vector<int> array{1, 2, 3, 4, 3, 2, 1};
head = myspace::generateSingleLL(&array);
cout << "input linked list: \n";
myspace::printSingleLL(head);
myspace::SingleLL *ptr_start, *ptr_end;
ptr_start = head;
ptr_end = head;
bool flag;
int counter = 0;
flag = check_palindrome(ptr_start, ptr_end, counter);
if (flag == true)
cout << "list is PALINDROME\n";
else
cout << "list is NOT PALINDROME\n";
return 0;
}