Challenge - 5 Problems
Circular Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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++
struct Node {
int data;
Node* next;
};
void printList(Node* head) {
if (!head) return;
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
std::cout << "null" << std::endl;
}
Node* deleteNode(Node* head, int key) {
if (!head) return nullptr;
if (head->data == key && head->next == head) {
delete head;
return nullptr;
}
Node *curr = head, *prev = nullptr;
while (curr->data != key) {
prev = curr;
curr = curr->next;
if (curr == head) return head; // key not found
}
if (curr == head) {
prev = head;
while (prev->next != head) prev = prev->next;
head = curr->next;
prev->next = head;
delete curr;
} else {
prev->next = curr->next;
delete curr;
}
return head;
}
int main() {
Node* head = new Node{1, nullptr};
Node* second = new Node{2, nullptr};
Node* third = new Node{3, nullptr};
Node* fourth = new Node{4, nullptr};
head->next = second;
second->next = third;
third->next = fourth;
fourth->next = head;
head = deleteNode(head, 3);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember that deleting a node removes it from the circle and links its previous node to the next node.
✗ Incorrect
The node with value 3 is removed. The list links 2 directly to 4, maintaining the circular structure. The print starts from head (1) and prints 1, 2, 4, then stops.
🧠 Conceptual
intermediateUnderstanding deletion of head node in circular linked list
If you delete the head node in a circular linked list, which step is necessary to maintain the circular structure?
Attempts:
2 left
💡 Hint
Think about what connects the last node back to the head.
✗ Incorrect
When the head node is deleted, the last node's next pointer must point to the new head to keep the list circular.
🔧 Debug
advancedIdentify the error in circular linked list deletion code
What error will this code cause when deleting a node from a circular linked list?
Node* deleteNode(Node* head, int key) {
if (!head) return nullptr;
Node* curr = head;
while (curr->data != key) {
curr = curr->next;
if (curr == head) return head;
}
Node* temp = curr->next;
curr->data = temp->data;
curr->next = temp->next;
delete temp;
return head;
}
Attempts:
2 left
💡 Hint
Consider what happens when the list has only one node.
✗ Incorrect
If the list has only one node and that node is deleted, the code tries to access temp->next which is invalid, causing a segmentation fault.
❓ Predict Output
advancedResult after deleting a non-head node in circular linked list
What is the printed state of the circular linked list after deleting the node with value 1?
DSA C++
struct Node {
int data;
Node* next;
};
void printList(Node* head) {
if (!head) return;
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
std::cout << "null" << std::endl;
}
Node* deleteNode(Node* head, int key) {
if (!head) return nullptr;
if (head->data == key && head->next == head) {
delete head;
return nullptr;
}
Node *curr = head, *prev = nullptr;
while (curr->data != key) {
prev = curr;
curr = curr->next;
if (curr == head) return head; // key not found
}
if (curr == head) {
prev = head;
while (prev->next != head) prev = prev->next;
head = curr->next;
prev->next = head;
delete curr;
} else {
prev->next = curr->next;
delete curr;
}
return head;
}
int main() {
Node* head = new Node{1, nullptr};
Node* second = new Node{2, nullptr};
Node* third = new Node{3, nullptr};
Node* fourth = new Node{4, nullptr};
head->next = second;
second->next = third;
third->next = fourth;
fourth->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
After deleting node 1 (head), the new head is node 2. The list prints 2, 3, 4 in order.
🧠 Conceptual
expertNumber of nodes after multiple deletions in circular linked list
Starting with a circular linked list of 5 nodes (values 10, 20, 30, 40, 50), after deleting nodes with values 10, 30, and 50, how many nodes remain?
Attempts:
2 left
💡 Hint
Count how many nodes are removed and subtract from the original count.
✗ Incorrect
Deleting 3 nodes from 5 leaves 2 nodes remaining. The remaining nodes are 20 and 40.
