Removing nodes with a given value from a linked list 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", &amp;x);

    if (head == NULL)
        exit(0);

    while(head-&gt;val == x){
        head = head-&gt;next;
        free(temp);
        temp = head;
    }

    curr = head;
    temp = curr-&gt;next;

    while (temp != NULL){
        if (temp-&gt;val == x){
            if(temp-&gt;next != NULL){
                curr-&gt;next = temp-&gt;next;
                free (temp);
                temp = curr-&gt;next; 
            }
            else {
                curr-&gt;next = NULL;
            }
        }
        else {
            curr = temp;
            temp = temp-&gt;next;
        }
    }

    return head;
}
The input asking for which value to delete can/should be placed in a separate function.  Then the function to remove the nodes can take that value as a parameter.

The two malloc calls for temp and curr near the top are completely unnecessary and just leak memory.  You want to remove nodes, so you shouldn't have to allocate any memory.

If your list consists of a single node where val == x (or, more generally, all nodes in the list have that value), your program will crash.

If the last node in the list matches the value (and there are other nodes that don't), you'd be leaking that node except that you will be stuck in an infinite loop.  You shouldn't need to handle the last node any differently than removing any other node.