Q:

Clean sequences from a LinkedList

Edited
I have to remove from a LinkedList sequences which their sum equals to 5.
for example for this Linked list: {3,2, 1, 4, 10,3,3,2,1} I get {10, 3, 3, 2, 1}. I dont really know why it's like that, it should remove 3,2 also but it dosent.
void LinkedList::cleanFive(LinkedList& list)
{
head = list.GetHead();

Node* end = head;
Node* headd = head;
Node* flag = head;
Node* curr = head;

int sum = 0;

while (end) {
    sum += end->data;

    if (sum < 5) {
        end = end->next;
    }
    if (sum == 5) {
        end = end->next;
        headd = end;
        flag = headd;
        head = flag;
        sum = 0;
    }
    if (sum > 5) {
        flag = head;
        curr = end;
        end = end->next;
        sum = 0;
    }
}

}

A:

curr = head;
end = head;
currprev = nullptr;

std::vector<Node*> flag;
while(end) {
     sum += end->data;
     if (sum < 5) {
         flag.push_back(end);
         end = end->next;
     }

     if (sum > 5) {
         currprev = curr;
         curr = curr->next;
         end = curr;
         sum = 0;
         empty_flag()
     }

     if (sum == 5) {
         curr = end->next;
         end = curr;
         sum = 0;
         delete_nodes_from_flag_from_linked_list()
         empty_flag()
     }

}
Here is a possible solution. flag is in this case a vector of nodes which are saved until the sum is proven to be 5 or greater as 5. With flag there are two operations which need to be made: empy_flag() - deletes all entries from the vector and delete_nodes_from_flag_from_linked_list() - that deletes the elements from flag from the linked list.
Code for delete_nodes_from_flag_from_linked_list() should be as follows:
auto it = flag.end();
it--;
Node* last = *it;

if (currprev)
    currprev->next = last->next;
else
    head = last->next

for (auto f : flag) 
    delete f;

where currprev is the element just before curr. You have to keep track of this element from the beginning, unless your linked list elements keep pointers to previous elements. I am updating the code from above. head is the start of the list.

