Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T>
- void push(box<T>*& head, const T& newData) {
- head = new box<T>(newData, head);
- }
- template <typename T>
- void push_back(box<T>*& head, const T& newData) {
- if (!head) {
- push(head, newData);
- return;
- }
- box<T>* temp = head;
- while (temp->next != nullptr) {
- temp = temp->next;
- }
- temp->next = new box<T>(newData);
- }
- box<int>* createReversedSimplified(box<int>* head) {
- if (head == nullptr) {
- return nullptr;
- }
- box<int>* temp = head;
- box<int>* result = nullptr;
- while (temp != nullptr) {
- push(result,temp->data);
- temp = temp->next;
- }
- return result;
- }
- void reverseSinglyLinkedList(box<int>*& head) {
- if (head == nullptr) {
- return;
- }
- if (head->next == nullptr) {
- return;
- }
- box<int>* last = head;
- box<int>* secondToLast = head;
- while (last->next != nullptr) {
- last = last->next;
- }
- box<int>* lastSave = last;
- while (secondToLast->next != last) {
- secondToLast = secondToLast->next;
- }
- last->next = secondToLast;
- while (secondToLast != head) {
- box<int>* it = head;
- while (it->next != secondToLast) {
- it = it->next;
- }
- last = secondToLast;
- secondToLast = it;
- last->next = secondToLast;
- }
- head->next = nullptr;
- head = lastSave;
- }
- void mirrorList(box<int>*& head) {
- if (head == nullptr) {
- return;
- }
- box<int>* reversed = createReversedSimplified(head);
- box<int>* last = head;
- while (last->next != nullptr) {
- last = last->next;
- }
- last->next = reversed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement