Challenge - 5 Problems
Delete Node at End Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after deleting the last node?
Given the linked list and the delete at end function, what will be the printed list after deletion?
DSA C
struct Node {
int data;
struct Node* next;
};
void deleteAtEnd(struct Node** head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node* temp = *head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->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 = NULL;
deleteAtEnd(&head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Think about which node is removed when deleting at the end.
✗ Incorrect
The function deletes the last node (which has data 3). So the list becomes 1 -> 2 -> null.
❓ Predict Output
intermediate2:00remaining
What happens if the list has only one node and we delete at end?
Consider a linked list with a single node containing 5. After calling deleteAtEnd, what is the output of printList?
DSA C
struct Node {
int data;
struct Node* next;
};
void deleteAtEnd(struct Node** head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node* temp = *head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("null\n");
}
int main() {
struct Node* head = malloc(sizeof(struct Node));
head->data = 5;
head->next = NULL;
deleteAtEnd(&head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
The only node is freed and head is set to NULL, so printList prints null.
🔧 Debug
advanced2:00remaining
Why does this deleteAtEnd function cause a crash?
Identify the problem in this deleteAtEnd function that causes a crash when deleting the last node in a list with two nodes.
DSA C
void deleteAtEnd(struct Node** head) {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
free(temp);
temp = NULL;
}Attempts:
2 left
💡 Hint
Think about what happens to the node before the last node after deletion.
✗ Incorrect
The function frees the last node but does not update the previous node's next pointer, so it still points to freed memory causing a crash.
🧠 Conceptual
advanced1:00remaining
What is the time complexity of deleting the last node in a singly linked list?
Given a singly linked list with n nodes, what is the time complexity of deleting the last node?
Attempts:
2 left
💡 Hint
You need to find the second last node before deleting the last node.
✗ Incorrect
Since singly linked lists do not have backward pointers, you must traverse from the head to find the second last node, which takes O(n) time.
🚀 Application
expert3:00remaining
After multiple deletions at end, what is the final list?
Starting with the list 10 -> 20 -> 30 -> 40 -> null, we call deleteAtEnd three times. What is the final printed list?
DSA C
struct Node {
int data;
struct Node* next;
};
void deleteAtEnd(struct Node** head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node* temp = *head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
void printList(struct Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->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 = NULL;
deleteAtEnd(&head);
deleteAtEnd(&head);
deleteAtEnd(&head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Each deletion removes the last node, so track the list after each call.
✗ Incorrect
After first deletion: 10 -> 20 -> 30 -> null
After second deletion: 10 -> 20 -> null
After third deletion: 10 -> null
