I'm trying to implement a linked list and according to LeakSanitizer I have a leak in my program in this function. The function inserts new nodes and keeps them sorted in ascending order.
typedef struct Node {
int data;
struct Node *next;
} node;
void insert(node **head, int num) {
node *temp_node = NULL, *curr = NULL;
temp_node = (node *)malloc(sizeof(node));
temp_node->data = num;
temp_node->next = NULL;
if (*head == NULL) {
*head = temp_node;
} else
if ((*head)->data >= temp_node->data) {
temp_node->next = *head;
*head = temp_node;
} else {
curr = *head;
while (curr->next != NULL && curr->next->data < temp_node->data)
curr = curr->next;
temp_node->next = curr->next;
curr->next = temp_node;
}
}
This is the call to insert in my main function
node *head;
...other code...
insert(&head, number);
...other code....
free(head);
Any advice is really appreciated.