This is my second iteration of a function to remove all nodes containing value 'x'. The previous iteration is here.
This function takes as input the head of a list and has to delete all the nodes that contain a given value taken. Is this an improvement on the previous iteration? Is there anything else that can be improved?
typedef struct node {
int val;
struct node *next;
} node_t;
node_t *rimuovi(node_t *head, int x){
node_t *temp = head;
node_t *curr = head;
if (head == NULL){
printf("Lista vuota, finito!\n");
return head;
}
while(head->val == x){
if (head->next == NULL){
free(head);
printf("La lista adesso e' vuota. Finito\n");
return head;
}
else {
curr = head->next;
free(head);
head = curr;
}
}
temp = head;
curr = temp->next;
while (curr != NULL){
if (curr->val == x){
if(curr->next != NULL){
temp->next = curr->next;
free (curr);
curr = temp->next;
}
else {
temp->next = NULL;
}
}
else {
temp = curr;
curr = curr->next;
}
}
return head;
}