Working C code dealing with lists: problem with memory leak I have written a working code that generates a simple linked list of dates and associated holidays. I realize that I have a problem with memory leaking as I copy lists repeatedly, so I wrote a freeList function. I am just having issues determining where to place this function. 

//Creates a linked list of dates/holidays

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

typedef struct hashElem{
    char* date;
    char* holiday;
    struct hashElem* next;

}hashElem;

// Initialize list with input value
hashElem* createNode(char* input_date, char* input_holiday)
{  
    hashElem* ptr = malloc(sizeof(*ptr));

    if (ptr == NULL) 
    {
        perror("Error");
        return NULL;
    }

    ptr -&gt; date = input_date;
    ptr -&gt; holiday = input_holiday;
    ptr -&gt; next = NULL;

    return ptr;
}

void freeList(hashElem* head)
{
    hashElem* ptr = head;
    while (head != NULL)
    {
        ptr = ptr-&gt;next;
        free(head);
        head = ptr;
    }
}

hashElem* addToTail(hashElem* head, char* input_date, char* input_holiday)
{
    hashElem* ptr = malloc(sizeof(*ptr));
    hashElem* temp = head; 

    // If there is not yet a list, initialize the list with the input value
    if (head == NULL)
        return createNode(input_date, input_holiday);

    if (ptr == NULL)
        return NULL;
    else
    {
        ptr-&gt;date = input_date;
        ptr-&gt;holiday = input_holiday;
        ptr-&gt;next = NULL;   

        // Find the last element of the list
        while (temp-&gt;next != NULL)
            temp = temp-&gt;next;

        // Add new node to the end of the list
        temp-&gt;next = ptr;
        ptr = head;

        //free(head);
        return ptr;
    }
}

hashElem* holidayList(void)
{
    hashElem* List = NULL;
    List = addToTail(List, "01/01/13", "New Years Day");
    List = addToTail(List, "14/02/13", "Valentine's Day");
    List = addToTail(List, "26/03/13", "Start of Passover");
    List = addToTail(List, "16/06/13", "Father's Day");
    List = addToTail(List, "31/10/13", "Halloween");
    List = addToTail(List, "28/11/13", "Thanksgiving");
    List = addToTail(List, "25/12/13", "Christmas");

    return List;
}


void printList(hashElem* head)
{
    hashElem* ptr = head;

    printf("------------------------------\n");
    printf("PRINTING LINKED LIST\n");
    while (ptr != NULL)
    {
        printf("Date: %s,  Holiday: %s\n", ptr-&gt;date, ptr-&gt;holiday);
        ptr = ptr-&gt;next;
    }

    printf("------------------------------\n");
}
In your current code, I can't see the copying of lists repeatedly as you stated. You are using pointers here so you're not actually duplicating the list.

The freeList function should be called once you're done using your list (the one returned by holidayList()).

Side Note:
You can refactor the code by utilizing createNode function when creating nodes and adding it to the tail.