Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that deleting at position 3 removes the third node from the list.
✗ Incorrect
The node at position 3 has the value 30. After deleting it, the list links 20 directly to 40, so the list becomes 10 -> 20 -> 40 -> 50 -> NULL.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Deleting position 1 removes the head node.
✗ Incorrect
Deleting the head node (value 5) moves the head pointer to the next node (value 15). The list becomes 15 -> 25 -> 35 -> NULL.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Consider what happens if temp or temp->next is NULL before accessing temp->next->next.
✗ Incorrect
If position is greater than the list length, temp or temp->next becomes NULL. Accessing temp->next->next causes a segmentation fault (crash).
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Deleting the last node removes the tail of the list.
✗ Incorrect
The node at position 4 has value 4. After deletion, the list ends at node with value 3, so the list is 1 -> 2 -> 3 -> NULL.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Consider how you find the node before deleting it in a singly linked list.
✗ Incorrect
To delete a node at a specific position, you must traverse the list up to that position, which takes O(n) time. Deletion itself is O(1) once the node is found.
