-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPalindromeList.cpp
134 lines (107 loc) · 2.83 KB
/
PalindromeList.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
// https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/772/
#include <iostream>
#include <vector>
class Node
{
public:
int key;
Node* next;
Node(int x) : key {x}, next{nullptr} {}
};
class LinkedList
{
private:
Node* head;
public:
LinkedList() : head{nullptr} {}
void printList() {
if (head == nullptr) {
return;
}
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->key << " ";
temp = temp->next;
}
std::cout << "\n";
}
void printList(Node* node) {
if (node == nullptr) {
return;
}
while (node != nullptr) {
std::cout << node->key << " ";
node = node->next;
}
std::cout << "\n";
}
void addFront(int key) {
Node* node = new Node(key);
if (head != nullptr) {
node->next = head;
}
head = node;
}
void addKeys(const std::vector<int> keys) {
for (auto key : keys) {
addFront(key);
}
}
Node* reverseList(Node* head) {
Node* previous = nullptr;
Node* next = nullptr;
while (head != nullptr) {
next = head->next;
head->next = previous;
previous = head;
head = next;
}
return previous;
}
bool isPalindrome() {
if (head == nullptr || head->next == nullptr) {
return true;
}
Node* slow = head;
Node* fast = head;
while (fast->next != nullptr && fast->next->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
Node* mid = slow;
slow->next = reverseList(slow->next);
slow = slow->next;
Node* nextToMid = slow;
Node* temp = head;
while (slow != nullptr) {
if (temp->key != slow->key) {
mid->next = reverseList(nextToMid);
return false;
}
temp = temp->next;
slow = slow->next;
}
mid->next = reverseList(nextToMid);
return true;
}
};
int main()
{
std::vector<int> keys {5, 1, 0, -3, -99};
LinkedList list;
std::cout << "List : ";
list.addKeys(keys);
list.printList();
std::cout << "Is list palindrome: " << std::boolalpha << list.isPalindrome() << "\n";
std::cout << "List after call: ";
list.printList();
std::cout << "============================================\n";
std::vector<int> keys2 {1, 2, 2, 1};
LinkedList list2;
std::cout << "List : ";
list2.addKeys(keys2);
list2.printList();
std::cout << "Is list palindrome: " << std::boolalpha << list2.isPalindrome() << "\n";
std::cout << "List after call: ";
list2.printList();
}