I'm doing this homework exercise with simple linked lists in C with nodes having the following structure:
typedef struct lista { int num; struct lista * next; } nodo;
So the exercise asks us to traverse the list and if we find two nodes such that node1->num == P and node2->num == Q, we delete all nodes between them and also them.
The "easy" way of doing this is by just making the previous node of the one that holds P point to the node that comes after the one that holds Q (Let's assume for the sake of the exercise that the node holding Q is not the last one).
But I know this is wrong...the now unused nodes are still occupying memory. I have to use free() somehow but I don't what pattern or best practice to use here.
EDIT: Previously I wrote keeping an array. This would not work as I don't know how long the list will be...so I would need to create another simple linked with each node holding the pointer to the ones that will be deleted, correct?