Question: Create a function compress, which takes in a pointer to the head of the linked list, and returns a pointer to the head of a
Create a function compress, which takes in a pointer to the head of the linked list, and returns a pointer to the head of a linked list in which repetition has been eliminated.
The linked list for this problem uses the following class declaration:
class node { public: string data; node* next; }; Example:
"green"->"green"->"blue"->"red"->"green"->NULL return "green"->"blue"->"red"->"green"->NULL
"apple"->"apple"->NULL return "apple"->NULL
Do this in C++. Code in bold CANNOT be changed.
#include
node* compress(node* head) { if (head == NULL) return NULL;
node *curr = head; node *aux;
while (curr->next != NULL) { if (curr->data == curr->next->data) { aux = curr->next->next; delete curr->next; curr->next = aux; } else { curr = curr->next; } } head = curr;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
