Challenge - 5 Problems
Circular 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 a node from circular linked list
What is the printed state of the circular linked list after deleting the node with value 3?
DSA C
/* Circular linked list node structure */ #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; /* Function to print circular linked list */ void printList(Node* head) { if (!head) return; Node* temp = head; do { printf("%d -> ", temp->data); temp = temp->next; } while (temp != head); printf("null\n"); } /* Function to delete node with given key */ Node* deleteNode(Node* head, int key) { if (!head) return NULL; Node *curr = head, *prev = NULL; // If head is to be deleted and only one node if (head->data == key && head->next == head) { free(head); return NULL; } // Find the node to delete do { if (curr->data == key) { if (prev) { prev->next = curr->next; if (curr == head) head = curr->next; free(curr); return head; } else { // Deleting head node Node* last = head; while (last->next != head) last = last->next; last->next = head->next; Node* temp = head; head = head->next; free(temp); return head; } } prev = curr; curr = curr->next; } while (curr != head); return head; // Key not found } int main() { Node* head = malloc(sizeof(Node)); Node* second = malloc(sizeof(Node)); Node* third = malloc(sizeof(Node)); Node* fourth = malloc(sizeof(Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = fourth; fourth->data = 4; fourth->next = head; head = deleteNode(head, 3); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Remember that deleting a node removes it and links its previous node to the next node.
✗ Incorrect
The node with value 3 is removed. The list links 2 directly to 4, so the printed list is 1 -> 2 -> 4 -> null.
❓ Predict Output
intermediate2:00remaining
Output after deleting the head node in circular linked list
What is the printed state of the circular linked list after deleting the node with value 1 (head node)?
DSA C
/* Same node structure and functions as before */ int main() { Node* head = malloc(sizeof(Node)); Node* second = malloc(sizeof(Node)); Node* third = malloc(sizeof(Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = head; head = deleteNode(head, 1); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Deleting the head node means the new head is the next node.
✗ Incorrect
The head node with value 1 is deleted. The last node (3) points to the new head (2). The printed list is 2 -> 3 -> null.
❓ Predict Output
advanced2:00remaining
Output after deleting the only node in circular linked list
What is the output after deleting the only node (value 10) in a circular linked list?
DSA C
/* Node structure and functions as before */ int main() { Node* head = malloc(sizeof(Node)); head->data = 10; head->next = head; head = deleteNode(head, 10); if (!head) printf("List is empty\n"); else printList(head); return 0; }
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
After deleting the only node, the list becomes empty (head is NULL). The program prints 'List is empty'.
❓ Predict Output
advanced2:00remaining
Output after deleting a non-existent node
What is the printed state of the circular linked list after trying to delete a node with value 5 which does not exist?
DSA C
/* Node structure and functions as before */ int main() { Node* head = malloc(sizeof(Node)); Node* second = malloc(sizeof(Node)); Node* third = malloc(sizeof(Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = head; head = deleteNode(head, 5); printList(head); return 0; }
Attempts:
2 left
💡 Hint
If the node to delete is not found, the list remains unchanged.
✗ Incorrect
Since node with value 5 does not exist, the list remains unchanged and prints 1 -> 2 -> 3 -> null.
🧠 Conceptual
expert2:00remaining
Why is it necessary to update the last node's next pointer when deleting the head in a circular linked list?
Choose the best explanation for why the last node's next pointer must be updated when deleting the head node in a circular linked list.
Attempts:
2 left
💡 Hint
Think about what keeps the circular linked list connected in a loop.
✗ Incorrect
In a circular linked list, the last node's next pointer points to the head node. When the head is deleted, this pointer must be updated to point to the new head to keep the list circular.
