Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What happens when you delete a node at position 1 in a singly linked list?
easy
A. The list remains unchanged
B. The last node is removed
C. The head node is removed and the next node becomes the new head
D. All nodes are deleted

Solution

  1. Step 1: Understand position 1 in linked list

    Position 1 refers to the head node of the list.
  2. Step 2: Deleting the head node

    Removing the head means the next node becomes the new head.
  3. Final Answer:

    The head node is removed and the next node becomes the new head -> Option C
  4. Quick Check:

    Deleting position 1 removes head [OK]
Hint: Deleting position 1 always removes the head node [OK]
Common Mistakes:
  • Thinking position 1 deletes the last node
  • Assuming list stays same after deletion
  • Deleting all nodes instead of one
2. Which of the following is the correct syntax to delete a node at position pos in a singly linked list in C?
easy
A. void deleteNode(struct Node* head, int pos);
B. void deleteNode(struct Node** head_ref, int pos);
C. void deleteNode(int pos, struct Node* head);
D. void deleteNode(int pos);

Solution

  1. Step 1: Understand pointer usage for head modification

    To delete a node, the function must modify the head pointer if position 1 is deleted, so it needs a pointer to pointer.
  2. Step 2: Check function parameters

    Only void deleteNode(struct Node** head_ref, int pos); uses struct Node** head_ref allowing head modification.
  3. Final Answer:

    void deleteNode(struct Node** head_ref, int pos); -> Option B
  4. Quick Check:

    Use pointer to pointer for head changes [OK]
Hint: Use double pointer to head to delete at any position [OK]
Common Mistakes:
  • Using single pointer and expecting head to change
  • Wrong parameter order
  • Missing head pointer in parameters
3. Given the linked list: 10 -> 20 -> 30 -> 40 -> 50 -> NULL, what is the list after deleting the node at position 3?
medium
A. 10 -> 20 -> 40 -> 50 -> NULL
B. 10 -> 20 -> 30 -> 50 -> NULL
C. 10 -> 30 -> 40 -> 50 -> NULL
D. 20 -> 30 -> 40 -> 50 -> NULL

Solution

  1. Step 1: Identify node at position 3

    Position 3 node is 30 in the list.
  2. Step 2: Remove node 30 and link previous to next

    Link node 20 to node 40, skipping 30.
  3. Final Answer:

    10 -> 20 -> 40 -> 50 -> NULL -> Option A
  4. Quick Check:

    Deleted 3rd node (30) correctly [OK]
Hint: Skip the node at position by linking previous to next [OK]
Common Mistakes:
  • Deleting wrong node position
  • Not updating links correctly
  • Removing head instead of position 3
4. Consider this code snippet to delete a node at position pos in a linked list:
void deleteNode(struct Node** head_ref, int pos) {
  struct Node* temp = *head_ref;
  if (pos == 1) {
    *head_ref = temp->next;
    free(temp);
    return;
  }
  for (int i = 1; temp != NULL && i < pos - 1; i++) {
    temp = temp->next;
  }
  if (temp == NULL || temp->next == NULL) return;
  struct Node* next = temp->next->next;
  free(temp->next);
  temp->next = next;
}
What is the bug in this code?
medium
A. It does not handle empty list when pos is 1
B. It frees the wrong node
C. It does not update head pointer correctly
D. It does not check if pos is valid

Solution

  1. Step 1: Check deletion at position 1

    If list is empty (*head_ref is NULL), temp is NULL, so temp->next causes error.
  2. Step 2: Identify missing empty list check

    Code should check if temp is NULL before accessing temp->next when pos == 1.
  3. Final Answer:

    It does not handle empty list when pos is 1 -> Option A
  4. Quick Check:

    Empty list check missing at head deletion [OK]
Hint: Always check if list is empty before deleting [OK]
Common Mistakes:
  • Ignoring empty list case
  • Assuming pos is always valid
  • Not freeing the deleted node
5. You have a singly linked list: 5 -> 10 -> 15 -> 20 -> 25 -> NULL. You want to delete the node at position 6. What should happen after calling the delete function?
hard
A. The last node (25) is deleted
B. The function crashes due to invalid position
C. The head node (5) is deleted
D. The list remains unchanged because position 6 is invalid

Solution

  1. Step 1: Check if position 6 exists in list

    List has 5 nodes, so position 6 is invalid.
  2. Step 2: Behavior on invalid position

    Proper delete function should do nothing if position is invalid, leaving list unchanged.
  3. Final Answer:

    The list remains unchanged because position 6 is invalid -> Option D
  4. Quick Check:

    Invalid position means no deletion [OK]
Hint: Invalid position means no change to list [OK]
Common Mistakes:
  • Deleting last node incorrectly
  • Crashing due to no position check
  • Deleting head by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard