forked from Silver-Taurus/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listPallindrome.cpp
148 lines (130 loc) · 2.91 KB
/
listPallindrome.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
/**
* Given a linkedList, determine the data it has are in pallindrome
* a->b->c->c->b->a
* r->a->d->a->r
*/
#include <iostream>
struct Node {
char data;
Node * next;
Node( char c ) : data{ c }, next{ nullptr } { }
};
void insert( Node * & head, char c )
{
Node * newNode = new Node(c);
if ( head == nullptr ) {
head = newNode;
} else {
Node * temp = head;
while ( temp->next != nullptr ) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList( Node * head )
{
while( head ) {
std::cout << head->data << " --> ";
head = head->next;
}
std::cout << " NULL" << std::endl;
}
void reverseList( Node * & head )
{
if ( head == nullptr ||
(head && (head->next == nullptr))) {
return;
}
Node * nextNode = nullptr;
Node * newHead = nullptr;
while ( head ) {
nextNode = head->next;
head->next = newHead;
newHead = head;
head = nextNode;
}
head = newHead;
}
bool isPallindrome( Node * head )
{
if ( head == nullptr || (head && head->next == nullptr)) {
return true;
}
Node * slowPtr = head;
Node * fastPtr = head;
while ( slowPtr && fastPtr && fastPtr->next )
{
fastPtr = fastPtr->next->next;
slowPtr = slowPtr->next;
}
//case of odd number of data, we skip the middle node.
if ( fastPtr && fastPtr->next == nullptr ) {
slowPtr = slowPtr->next;
}
//reversing second half of list
reverseList( slowPtr );
Node * ptr1 = head;
Node * ptr2 = slowPtr;
while( ptr1 && ptr2 && ptr1->data == ptr2->data) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
//reversing second half back
reverseList( slowPtr );
if ( ptr2 == nullptr ) {
return true;
} else {
return false;
}
}
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 ( isPallindrome( 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 ( isPallindrome( 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 ( isPallindrome( 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;
}