Skip to content

Commit

Permalink
browser list
Browse files Browse the repository at this point in the history
  • Loading branch information
imashiqe committed Oct 26, 2024
1 parent bec3e1e commit 77b1ace
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
Empty file.
54 changes: 54 additions & 0 deletions datastucture/mid/palindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>

struct Node {
int data;
Node* next;
Node* prev;
Node(int val) : data(val), next(nullptr), prev(nullptr) {}
};
class Solution{
public:
Node* head;
Node* tail;
Solution() : head(nullptr), tail(nullptr) {}

void append(int val) {
Node* newNode = new Node(val);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}

bool isPalindrome() {
if (!head || !head->next) return true;

Node* left = head;
Node* right = tail;

while (left != right && left->prev != right) {
if (left->data != right->data) return false;
left = left->next;
right = right->prev;
}
return true;
}
};

int main() {
DoublyLinkedList dll;
int value;
while (std::cin >> value && value != -1) {
dll.append(value);
}
if (dll.isPalindrome()) {
std::cout << "YES" << std::endl;
} else {
std::cout << "NO" << std::endl;
}

return 0;
}
21 changes: 21 additions & 0 deletions datastucture/mid/remove_duolicate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <list>
#include <set>

using namespace std ;

int main() {
std::list<int> LinkedList;
int value;
while (std::cin >> value && value != -1) {
LinkedList.push_back(value);
}
std::set<int> uniqueValues(LinkedList.begin(), LinkedList.end());
LinkedList.clear();
LinkedList.insert(LinkedList.begin(), uniqueValues.begin(), uniqueValues.end());

for (int val : LinkedList) {
std::cout << val << " ";
}
return 0;
}

0 comments on commit 77b1ace

Please sign in to comment.