Is this a good way of removing all the nodes that contains a value x? This function takes as input the head of a list and has to delete all the nodes that contain a given value taken. If there is any better way of doing this can you please show them to me?
node_t *funzione(node_t *head){
int x;
node_t *temp = head;
node_t *curr = head;
temp = (node_t *)malloc(sizeof(node_t));
curr = (node_t *)malloc(sizeof(node_t));
printf("Inserisci il valore che vuoi eliminare: \n");
scanf("%d", &x);
if (head == NULL)
exit(0);
while(head->val == x){
head = head->next;
free(temp);
temp = head;
}
curr = head;
temp = curr->next;
while (temp != NULL){
if (temp->val == x){
if(temp->next != NULL){
curr->next = temp->next;
free (temp);
temp = curr->next;
}
else {
curr->next = NULL;
}
}
else {
curr = temp;
temp = temp->next;
}
}
return head;
}
node_t. \$\endgroup\$