Skip to main content

HACKERRANK -- DELETE A NODE AT A GIVEN POSITION

 

HACKERRANK --  DELETE A NODE AT A GIVEN POSITION


Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value.

Example


After removing the node at position .

Function Description

Complete the deleteNode function in the editor below.

deleteNode has the following parameters:
SinglyLinkedListNode pointer llist: a reference to the head node in the list
int position: the position of the node to remove

Returns
SinglyLinkedListNode pointer: a reference to the head of the modified list

Input Format

The first line of input contains an integer , the number of elements in the linked list.
Each of the next  lines contains an integer, the node data values in order.
The last line contains an integer, , the position of the node to delete.

Constraints

  • , where  is the  element of the linked list.

Sample Input

8
20
6
2
19
7
4
15
9
3

Sample Output

20 6 2 7 4 15 9

Solution


SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) {
    SinglyLinkedListNode* newnode=(SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode));
    SinglyLinkedListNode* temp=(SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode));
    newnode=llist;
    if(llist==NULL){
        return NULL;
    }
    else{
        for(int i=0;i<=position;i++){
            if(position==0){
                llist=llist->next;
            }
            else if(i==position-1){
                newnode->next=newnode->next->next;
            }
            else{
                newnode=newnode->next;
            }
        }
    return llist;
    }
}

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

    SinglyLinkedList* llist = malloc(sizeof(SinglyLinkedList));
    llist->head = NULL;
    llist->tail = NULL;

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

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

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

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

        insert_node_into_singly_linked_list(&llist, llist_item);
    }

    char* position_endptr;
    char* position_str = readline();
    int position = strtol(position_str, &position_endptr, 10);

    if (position_endptr == position_str || *position_endptr != '\0') { exit(EXIT_FAILURE); }

    SinglyLinkedListNode* llist1 = deleteNode(llist->head, position);

    char *sep = " ";

    print_singly_linked_list(llist1, sep, fptr);
    fprintf(fptr, "\n");

    free_singly_linked_list(llist1);

    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 < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);

        if (!data) { break; }

        alloc_length = new_length;
    }

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

    data = realloc(data, data_length);

    return data;
}

Comments

Popular posts from this blog

POST ORDER TRAVERSAL -- HACKERRANK SOLUTIONS

  Complete the   function in the editor below. It received   parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the   function. Constraints   Nodes in the tree    Output Format Print the tree's postorder traversal as a single line of space-separated values. Sample Input 1 \ 2 \ 5 / \ 3 6 \ 4 Sample Output 4 3 6 5 2 1 Explanation The postorder traversal is shown. Solution #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > struct  node {           int  data;      struct  node *left;      struct  node *ri...

TREE PREORDER TRAVERSAL -- HACKERRANK SOLUTIONS

  Complete the   function in the editor below, which has   parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the  preOrder  function. Constraints  Nodes in the tree  Output Format Print the tree's preorder traversal as a single line of space-separated values. Sample Input 1 \ 2 \ 5 / \ 3 6 \ 4 Sample Output 1 2 5 3 4 6 Explanation The preorder traversal of the binary tree is printed. Solution #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > struct  node {           int  data;      struct  node *left;      struct ...