Why is Hackerrank showing segmentation error for the following C code? : <p><strong>Hackerrank Problem Description:</strong></p>

<p>You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.</p>

<p><strong>Input Format</strong></p>

<p>You have to complete the SinglyLinkedListNode insertAtTail(SinglyLinkedListNode head, int data) method. It takes two arguments: the head of the linked list and the integer to insert at tail. You should not read any input from the stdin/console.</p>

<p>The input is handled by code editor and is as follows:
The first line contains an integer , denoting the elements of the linked list.
The next  lines contain an integer each, denoting the element that needs to be inserted at tail.</p>

<p><strong>Constraints</strong></p>

<p><strong>Output Format</strong></p>

<p>Insert the new node at the tail and just return the head of the updated linked list. Do not print anything to stdout/console.</p>

<p>The output is handled by code in the editor and is as follows:
Print the elements of the linked list from head to tail, each in a new line.</p>

<p><strong>Sample Input</strong></p>

<p>5
141
302
164
530
474</p>

<p><strong>Sample Output</strong></p>

<p>141
302
164
530
474</p>

<p><strong>Explanation</strong></p>

<p>First, the linked list is NULL. </p>

<p>After inserting 141, the list is 141 -> NULL.</p>

<p>After inserting 302, the list is 141 -> 302 -> NULL.</p>

<p>After inserting 164, the list is 141 -> 302 -> 164 -> NULL.</p>

<p>After inserting 530, the list is 141 -> 302 -> 164 -> 530 -> NULL. </p>

<p>After inserting 474, the list is 141 -> 302 -> 164 -> 530 -> 474 -> NULL, which is the final list.</p>

<p><strong>My code:</strong></p>

<pre><code>#include &lt;assert.h&gt;
#include &lt;limits.h&gt;
#include &lt;math.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;stddef.h&gt;
#include &lt;stdint.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

char* readline();

typedef struct SinglyLinkedListNode SinglyLinkedListNode;
typedef struct SinglyLinkedList SinglyLinkedList;

struct SinglyLinkedListNode {
    int data;
    SinglyLinkedListNode* next;
};

struct SinglyLinkedList {
    SinglyLinkedListNode* head;
};

SinglyLinkedListNode* create_singly_linked_list_node(int node_data) {
    SinglyLinkedListNode* node = malloc(sizeof(SinglyLinkedListNode));

    node-&gt;data = node_data;
    node-&gt;next = NULL;

    return node;
}

void print_singly_linked_list(SinglyLinkedListNode* node, char* sep, FILE* fptr) {
    while (node) {
        fprintf(fptr, "%d", node-&gt;data);

        node = node-&gt;next;

        if (node) {
            fprintf(fptr, "%s", sep);
        }
    }
}

void free_singly_linked_list(SinglyLinkedListNode* node) {
    while (node) {
        SinglyLinkedListNode* temp = node;
        node = node-&gt;next;

        free(temp);
    }
}

// Complete the insertNodeAtTail function below.

/*
 * For your reference:
 *
 * SinglyLinkedListNode {
 *     int data;
 *     SinglyLinkedListNode* next;
 * };
 *
 */
//This is the required function which I have written
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
SinglyLinkedListNode *newNode = (SinglyLinkedListNode*)malloc (sizeof(SinglyLinkedListNode));
 SinglyLinkedListNode *p = head;
while(p-&gt;next!=NULL){
    p=p-&gt;next;
}
newNode-&gt;data = data;
newNode -&gt;next = p-&gt;next;
p -&gt;next = newNode;
return head;

}


int main()
{
    FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

    SinglyLinkedList* llist = malloc(sizeof(SinglyLinkedList));
    llist-&gt;head = NULL;

    char* llist_count_endptr;
    char* llist_count_str = readline();
    int llist_count = strtol(llist_count_str, &amp;llist_count_endptr, 10);

    if (llist_count_endptr == llist_count_str || *llist_count_endptr != '\0') { exit(EXIT_FAILURE); }

    for (int i = 0; i &lt; llist_count; i++) {
        char* llist_item_endptr;
        char* llist_item_str = readline();
        int llist_item = strtol(llist_item_str, &amp;llist_item_endptr, 10);

        if (llist_item_endptr == llist_item_str || *llist_item_endptr != '\0') { exit(EXIT_FAILURE); }

        SinglyLinkedListNode* llist_head = insertNodeAtTail(llist-&gt;head, llist_item);
        llist-&gt;head = llist_head;
    }



    char *sep = "\n";

    print_singly_linked_list(llist-&gt;head, sep, fptr);
    fprintf(fptr, "\n");

    free_singly_linked_list(llist-&gt;head);

    fclose(fptr);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) {
            break;
        }

        data_length += strlen(cursor);

        if (data_length &lt; alloc_length - 1 || data[data_length - 1] == '\n') {
            break;
        }

        alloc_length &lt;&lt;= 1;

        data = realloc(data, alloc_length);

        if (!line) {
            break;
        }
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';

        data = realloc(data, data_length);
    } else {
        data = realloc(data, data_length + 1);

        data[data_length] = '\0';
    }

    return data;
}
</code></pre>

<p><strong>Output:</strong>
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.</p>
