Bird
0
0
DSA Cprogramming~20 mins

Delete Node at Specific Position in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after deleting node at position 3
What is the printed linked list after deleting the node at position 3?
DSA C
/* Linked list: 10 -> 20 -> 30 -> 40 -> 50 -> NULL */

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

void deleteNodeAtPosition(struct Node** head_ref, int position) {
    if (*head_ref == NULL) return;
    struct Node* temp = *head_ref;
    if (position == 1) {
        *head_ref = temp->next;
        free(temp);
        return;
    }
    for (int i = 1; temp != NULL && i < position - 1; i++) {
        temp = temp->next;
    }
    if (temp == NULL || temp->next == NULL) return;
    struct Node* next = temp->next->next;
    free(temp->next);
    temp->next = next;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    head->data = 10;
    head->next = malloc(sizeof(struct Node));
    head->next->data = 20;
    head->next->next = malloc(sizeof(struct Node));
    head->next->next->data = 30;
    head->next->next->next = malloc(sizeof(struct Node));
    head->next->next->next->data = 40;
    head->next->next->next->next = malloc(sizeof(struct Node));
    head->next->next->next->next->data = 50;
    head->next->next->next->next->next = NULL;

    deleteNodeAtPosition(&head, 3);
    printList(head);
    return 0;
}
A10 -> 20 -> 30 -> 40 -> NULL
B10 -> 20 -> 30 -> 50 -> NULL
C10 -> 20 -> 40 -> 50 -> NULL
D20 -> 30 -> 40 -> 50 -> NULL
Attempts:
2 left
💡 Hint
Remember that deleting at position 3 removes the third node from the list.
Predict Output
intermediate
2:00remaining
Output after deleting node at position 1 (head)
What is the printed linked list after deleting the node at position 1 (the head)?
DSA C
/* Linked list: 5 -> 15 -> 25 -> 35 -> NULL */

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

void deleteNodeAtPosition(struct Node** head_ref, int position) {
    if (*head_ref == NULL) return;
    struct Node* temp = *head_ref;
    if (position == 1) {
        *head_ref = temp->next;
        free(temp);
        return;
    }
    for (int i = 1; temp != NULL && i < position - 1; i++) {
        temp = temp->next;
    }
    if (temp == NULL || temp->next == NULL) return;
    struct Node* next = temp->next->next;
    free(temp->next);
    temp->next = next;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    head->data = 5;
    head->next = malloc(sizeof(struct Node));
    head->next->data = 15;
    head->next->next = malloc(sizeof(struct Node));
    head->next->next->data = 25;
    head->next->next->next = malloc(sizeof(struct Node));
    head->next->next->next->data = 35;
    head->next->next->next->next = NULL;

    deleteNodeAtPosition(&head, 1);
    printList(head);
    return 0;
}
ANULL
B5 -> 25 -> 35 -> NULL
C5 -> 15 -> 35 -> NULL
D15 -> 25 -> 35 -> NULL
Attempts:
2 left
💡 Hint
Deleting position 1 removes the head node.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteNodeAtPosition function
What error will occur when running this code if position is greater than the length of the list?
DSA C
void deleteNodeAtPosition(struct Node** head_ref, int position) {
    if (*head_ref == NULL) return;
    struct Node* temp = *head_ref;
    if (position == 1) {
        *head_ref = temp->next;
        free(temp);
        return;
    }
    for (int i = 1; i < position - 1; i++) {
        temp = temp->next;
    }
    struct Node* next = temp->next->next;
    free(temp->next);
    temp->next = next;
}
ACompilation error due to missing semicolon
BSegmentation fault due to dereferencing NULL pointer
CMemory leak due to not freeing nodes
DNo error, deletion works fine
Attempts:
2 left
💡 Hint
Consider what happens if temp or temp->next is NULL before accessing temp->next->next.
Predict Output
advanced
2:00remaining
Output after deleting node at last position
What is the printed linked list after deleting the node at the last position (position 4)?
DSA C
/* Linked list: 1 -> 2 -> 3 -> 4 -> NULL */

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

void deleteNodeAtPosition(struct Node** head_ref, int position) {
    if (*head_ref == NULL) return;
    struct Node* temp = *head_ref;
    if (position == 1) {
        *head_ref = temp->next;
        free(temp);
        return;
    }
    for (int i = 1; temp != NULL && i < position - 1; i++) {
        temp = temp->next;
    }
    if (temp == NULL || temp->next == NULL) return;
    struct Node* next = temp->next->next;
    free(temp->next);
    temp->next = next;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    head->data = 1;
    head->next = malloc(sizeof(struct Node));
    head->next->data = 2;
    head->next->next = malloc(sizeof(struct Node));
    head->next->next->data = 3;
    head->next->next->next = malloc(sizeof(struct Node));
    head->next->next->next->data = 4;
    head->next->next->next->next = NULL;

    deleteNodeAtPosition(&head, 4);
    printList(head);
    return 0;
}
A1 -> 2 -> 3 -> NULL
B1 -> 2 -> 4 -> NULL
C2 -> 3 -> 4 -> NULL
D1 -> 3 -> 4 -> NULL
Attempts:
2 left
💡 Hint
Deleting the last node removes the tail of the list.
🧠 Conceptual
expert
2:00remaining
Time complexity of deleting node at specific position in singly linked list
What is the time complexity of deleting a node at a specific position in a singly linked list of size n?
AO(n) because traversal to the node is required before deletion
BO(1) because deletion is direct with pointer adjustment
CO(log n) because of binary search to find the node
DO(n^2) because each deletion requires scanning the entire list
Attempts:
2 left
💡 Hint
Consider how you find the node before deleting it in a singly linked list.