Bird
Raised Fist0
DSA Cprogramming~20 mins

Delete Node at End in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node at End Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A1 -> 2 -> null
B1 -> 2 -> 3 -> null
C1 -> null
Dnull
Attempts:
2 left
💡 Hint
Think about which node is removed when deleting at the end.
❓ Predict Output
intermediate
2: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;
}
A5 -> null
Bnull
CSegmentation fault
D5
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
🔧 Debug
advanced
2: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;
}
AThe loop goes to the last node, but we never update the previous node's next pointer to NULL.
BThe function frees the head pointer instead of the last node.
CThe function does not check if the list is empty before deleting.
DThe function frees the second last node instead of the last node.
Attempts:
2 left
💡 Hint
Think about what happens to the node before the last node after deletion.
🧠 Conceptual
advanced
1: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?
AO(n^2)
BO(1)
CO(log n)
DO(n)
Attempts:
2 left
💡 Hint
You need to find the second last node before deleting the last node.
🚀 Application
expert
3: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;
}
A10 -> 20 -> null
Bnull
C10 -> null
D20 -> null
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so track the list after each call.

Practice

(1/5)
1. What is the main purpose of the Delete Node at End operation in a singly linked list?
easy
A. To reverse the linked list
B. To add a new node at the end of the linked list
C. To delete the first node of the linked list
D. To remove the last node from the linked list

Solution

  1. Step 1: Understand the operation's goal

    The operation aims to remove the last node in the linked list, which is the node that points to null.
  2. Step 2: Identify the correct description

    Removing the last node means deleting the node at the end, not adding or reversing nodes.
  3. Final Answer:

    To remove the last node from the linked list -> Option D
  4. Quick Check:

    Delete Node at End = Remove last node [OK]
Hint: Deleting at end means removing the last node [OK]
Common Mistakes:
  • Confusing delete at end with delete at beginning
  • Thinking it adds a node instead of deleting
  • Mixing up with reversing the list
2. Which of the following C code snippets correctly deletes the last node of a singly linked list named head?
easy
A. if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL;
B. Node* temp = head; while (temp->next != NULL) temp = temp->next; free(temp);
C. free(head); head = NULL;
D. Node* temp = head; while (temp->next != NULL) temp = temp->next; temp->next = NULL;

Solution

  1. Step 1: Check for empty or single-node list and traverse to second last node

    if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; checks if list is empty or single node, then moves to the node before last using temp->next->next != NULL.
  2. Step 2: Free last node and update pointer

    It frees the last node and sets temp->next to NULL, correctly deleting the last node.
  3. Final Answer:

    if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; -> Option A
  4. Quick Check:

    Traverse to second last, free last, set next NULL = if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; [OK]
Hint: Stop at second last node before deleting last [OK]
Common Mistakes:
  • Freeing node without updating previous node's next pointer
  • Not checking for empty list
  • Freeing head without traversal
3. What will be the output linked list after deleting the last node from the list: 10 -> 20 -> 30 -> 40 -> NULL using the standard delete at end operation?
medium
A. 10 -> 20 -> 30 -> 40 -> NULL
B. 10 -> 20 -> 30 -> NULL
C. 20 -> 30 -> 40 -> NULL
D. 10 -> 20 -> NULL

Solution

  1. Step 1: Identify the last node

    The last node is 40, which points to NULL.
  2. Step 2: Remove the last node and update the second last node's next pointer

    After deletion, 30 becomes the last node and its next pointer is set to NULL.
  3. Final Answer:

    10 -> 20 -> 30 -> NULL -> Option B
  4. Quick Check:

    Last node 40 removed = 10 -> 20 -> 30 -> NULL [OK]
Hint: Last node removed, second last points to NULL [OK]
Common Mistakes:
  • Not updating second last node's next pointer
  • Deleting wrong node (like first node)
  • Output list unchanged
4. Consider the following code snippet to delete the last node of a singly linked list. What is the error?
void deleteEnd(Node** head) {
  if (*head == NULL) return;
  Node* temp = *head;
  while (temp->next != NULL) {
    temp = temp->next;
  }
  free(temp);
  temp = NULL;
}
medium
A. It does not update the second last node's next pointer to NULL
B. It frees the head node instead of the last node
C. It causes a segmentation fault by accessing NULL pointer
D. It deletes the first node instead of the last

Solution

  1. Step 1: Analyze traversal logic

    The loop moves temp to the last node (where temp->next is NULL).
  2. Step 2: Check pointer update after freeing

    It frees the last node but does not update the second last node's next pointer to NULL, leaving a dangling pointer.
  3. Final Answer:

    It does not update the second last node's next pointer to NULL -> Option A
  4. Quick Check:

    Missing next pointer update causes dangling pointer [OK]
Hint: Always update second last node's next to NULL after free [OK]
Common Mistakes:
  • Freeing node without updating previous node's next
  • Not handling empty list
  • Incorrect loop condition causing crash
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> 35 -> 45 -> NULL. Write the steps to delete the last node and then insert a new node with value 50 at the end. What will be the final list?
hard
A. 5 -> 15 -> 25 -> 35 -> NULL
B. 5 -> 15 -> 25 -> 35 -> 45 -> 50 -> NULL
C. 5 -> 15 -> 25 -> 35 -> 50 -> NULL
D. 50 -> 5 -> 15 -> 25 -> 35 -> NULL

Solution

  1. Step 1: Delete the last node (45)

    Traverse to second last node (35), free last node (45), set 35's next to NULL.
  2. Step 2: Insert new node with value 50 at end

    Traverse to last node (35), create new node with 50, set 35's next to new node, new node's next to NULL.
  3. Final Answer:

    5 -> 15 -> 25 -> 35 -> 50 -> NULL -> Option C
  4. Quick Check:

    Delete last, then add 50 at end = 5 -> 15 -> 25 -> 35 -> 50 -> NULL [OK]
Hint: Delete last node, then add new node at end [OK]
Common Mistakes:
  • Not updating pointers correctly after deletion
  • Appending new node without deleting last
  • Inserting new node at wrong position
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard