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 a node by value in a singly linked list
What is the printed linked list after deleting the node with value 3?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
Node* deleteNodeByValue(Node* head, int value) {
Node* temp = head;
Node* prev = NULL;
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return head;
if (prev == NULL) {
head = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 1;
head->next = malloc(sizeof(Node));
head->next->data = 2;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = malloc(sizeof(Node));
head->next->next->next->data = 4;
head->next->next->next->next = NULL;
head = deleteNodeByValue(head, 3);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
✗ Incorrect
The function searches for the node with value 3, removes it by changing the previous node's next pointer to skip it, then frees the node. The resulting list skips the node with value 3.
❓ Predict Output
intermediate2:00remaining
Output when deleting the head node by value
What is the printed linked list after deleting the node with value 1 (the head)?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
Node* deleteNodeByValue(Node* head, int value) {
Node* temp = head;
Node* prev = NULL;
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return head;
if (prev == NULL) {
head = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 1;
head->next = malloc(sizeof(Node));
head->next->data = 2;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = NULL;
head = deleteNodeByValue(head, 1);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting the head means the new head is the next node.
✗ Incorrect
Since the node with value 1 is the head, deleting it moves the head pointer to the next node, which has value 2.
🔧 Debug
advanced2:00remaining
Identify the error in deleting a node by value
What error will this code cause when deleting a node by value?
DSA C
Node* deleteNodeByValue(Node* head, int value) { Node* temp = head; Node* prev = NULL; while (temp->data != value) { prev = temp; temp = temp->next; } if (prev == NULL) { head = temp->next; } else { prev->next = temp->next; } free(temp); return head; }
Attempts:
2 left
💡 Hint
Consider what happens if the value is not found in the list.
✗ Incorrect
The code does not check if temp is NULL before accessing temp->data in the while loop condition. If the value is not found, temp becomes NULL and accessing temp->data causes a segmentation fault.
🧠 Conceptual
advanced1:00remaining
Effect of deleting a non-existent value in linked list
What happens if you try to delete a node with a value not present in the linked list?
Attempts:
2 left
💡 Hint
Think about the search process for the value.
✗ Incorrect
If the value is not found, the function returns the original head without modifying the list.
❓ Predict Output
expert3:00remaining
Output after multiple deletions by value in linked list
What is the printed linked list after deleting nodes with values 2 and 4 in order?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
Node* deleteNodeByValue(Node* head, int value) {
Node* temp = head;
Node* prev = NULL;
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return head;
if (prev == NULL) {
head = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 1;
head->next = malloc(sizeof(Node));
head->next->data = 2;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = malloc(sizeof(Node));
head->next->next->next->data = 4;
head->next->next->next->next = malloc(sizeof(Node));
head->next->next->next->next->data = 5;
head->next->next->next->next->next = NULL;
head = deleteNodeByValue(head, 2);
head = deleteNodeByValue(head, 4);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Delete nodes one by one and update the list each time.
✗ Incorrect
First deletion removes node with value 2, list becomes 1->3->4->5. Second deletion removes node with value 4, list becomes 1->3->5.
