Bird
Raised Fist0
DSA C++programming~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 Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting the last node from a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA C++
#include <iostream>
struct Node {
    int data;
    Node* next;
};

Node* deleteAtEnd(Node* head) {
    if (!head) return nullptr;
    if (!head->next) {
        delete head;
        return nullptr;
    }
    Node* temp = head;
    while (temp->next && temp->next->next) {
        temp = temp->next;
    }
    delete temp->next;
    temp->next = nullptr;
    return head;
}

void printList(Node* head) {
    while (head) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = new Node{1, new Node{2, new Node{3, nullptr}}};
    head = deleteAtEnd(head);
    printList(head);
    return 0;
}
Anull
B1 -> 2 -> 3 -> null
C1 -> null
D1 -> 2 -> null
Attempts:
2 left
💡 Hint
Think about how the last node is removed and what remains.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the last node from a single-node list
What is the printed state of the linked list after deleting the last node from a list with only one node?
DSA C++
#include <iostream>
struct Node {
    int data;
    Node* next;
};

Node* deleteAtEnd(Node* head) {
    if (!head) return nullptr;
    if (!head->next) {
        delete head;
        return nullptr;
    }
    Node* temp = head;
    while (temp->next && temp->next->next) {
        temp = temp->next;
    }
    delete temp->next;
    temp->next = nullptr;
    return head;
}

void printList(Node* head) {
    while (head) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = new Node{10, nullptr};
    head = deleteAtEnd(head);
    printList(head);
    return 0;
}
Anull
BSegmentation fault
C10 -> null
D10 -> 10 -> null
Attempts:
2 left
💡 Hint
What happens when you delete the only node in the list?
🔧 Debug
advanced
2:00remaining
Identify the error in deleting the last node
What error will occur when running this code to delete the last node?
DSA C++
#include <iostream>
struct Node {
    int data;
    Node* next;
};

Node* deleteAtEnd(Node* head) {
    if (!head) return nullptr;
    Node* temp = head;
    while (temp->next) {
        temp = temp->next;
    }
    delete temp;
    temp = nullptr;
    return head;
}

void printList(Node* head) {
    while (head) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = new Node{1, new Node{2, new Node{3, nullptr}}};
    head = deleteAtEnd(head);
    printList(head);
    return 0;
}
ASegmentation fault due to dangling pointer access
BMemory leak due to not deleting last node
CNo error, prints: 1 -> 2 -> null
DCompilation error due to missing return statement
Attempts:
2 left
💡 Hint
Check how the last node is deleted and what happens to the previous node's next pointer.
🧠 Conceptual
advanced
2:00remaining
Why update the second last node's next pointer when deleting the last node?
Why is it necessary to set the second last node's next pointer to nullptr after deleting the last node in a singly linked list?
ATo allow traversal to continue beyond the last node
BTo avoid memory leaks by freeing the last node's memory
CTo prevent the list from having a dangling pointer to deleted memory
DTo keep the list sorted after deletion
Attempts:
2 left
💡 Hint
Think about what happens if the pointer still points to deleted memory.
🚀 Application
expert
3:00remaining
Resulting list after multiple deletions at end
Given the initial list 5 -> 10 -> 15 -> 20 -> null, what is the printed list after deleting the last node three times in a row?
DSA C++
#include <iostream>
struct Node {
    int data;
    Node* next;
};

Node* deleteAtEnd(Node* head) {
    if (!head) return nullptr;
    if (!head->next) {
        delete head;
        return nullptr;
    }
    Node* temp = head;
    while (temp->next && temp->next->next) {
        temp = temp->next;
    }
    delete temp->next;
    temp->next = nullptr;
    return head;
}

void printList(Node* head) {
    while (head) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = new Node{5, new Node{10, new Node{15, new Node{20, nullptr}}}};
    head = deleteAtEnd(head);
    head = deleteAtEnd(head);
    head = deleteAtEnd(head);
    printList(head);
    return 0;
}
A5 -> 10 -> null
B5 -> null
C5 -> 10 -> 15 -> null
Dnull
Attempts:
2 left
💡 Hint
Each deletion removes the last node. Track the list after each deletion.

Practice

(1/5)
1. What happens when you delete the last node in a singly linked list?
easy
A. The second last node's next pointer becomes null
B. The head pointer is set to null
C. The first node is deleted instead
D. All nodes are deleted

Solution

  1. Step 1: Understand the linked list structure

    Each node points to the next node. The last node points to null.
  2. Step 2: Effect of deleting the last node

    Deleting the last node means the previous node's next pointer must be set to null to mark new end.
  3. Final Answer:

    The second last node's next pointer becomes null -> Option A
  4. Quick Check:

    Last node removed, previous node points to null [OK]
Hint: Last node removal sets previous node's next to null [OK]
Common Mistakes:
  • Setting head to null incorrectly
  • Deleting the first node instead
  • Not updating the previous node's next pointer
2. Which of the following is the correct syntax to delete the last node in a singly linked list in C++?
easy
A. delete lastNode->prev; lastNode->prev = nullptr;
B. delete lastNode; lastNode = nullptr;
C. delete head; head = nullptr;
D. delete lastNode->next; lastNode->next = nullptr;

Solution

  1. Step 1: Identify the last node's pointer

    In singly linked list, last node's previous node points to it via next pointer.
  2. Step 2: Delete last node and update pointer

    Deleting lastNode->next and setting lastNode->next to nullptr correctly removes last node.
  3. Final Answer:

    delete lastNode->next; lastNode->next = nullptr; -> Option D
  4. Quick Check:

    Delete next node and set pointer null [OK]
Hint: Delete node via previous node's next pointer [OK]
Common Mistakes:
  • Deleting head instead of last node
  • Using prev pointer in singly linked list
  • Not setting pointer to nullptr after delete
3. What will be the output linked list after deleting the last node from the list: 10 -> 20 -> 30 -> 40 -> null?
medium
A. 10 -> 20 -> null
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> 40 -> null
D. 10 -> 20 -> 30 -> 40 -> null

Solution

  1. Step 1: Identify the last node

    The last node is 40, which points to null.
  2. Step 2: Remove last node and update previous node

    Deleting 40 means 30's next pointer becomes null, ending list at 30.
  3. Final Answer:

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

    Last node 40 removed, list ends at 30 [OK]
Hint: Last node removed, previous node points to null [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting head node by mistake
  • Outputting original list without deletion
4. Given the code snippet below, what is the error when deleting the last node?
struct Node {
  int data;
  Node* next;
};

void deleteLast(Node*& head) {
  if (head == nullptr) return;
  Node* temp = head;
  while (temp->next != nullptr) {
    temp = temp->next;
  }
  delete temp;
  temp = nullptr;
}
medium
A. Uses wrong pointer type for deletion
B. Deletes head node instead of last node
C. Does not update previous node's next pointer to null
D. No error, code works correctly

Solution

  1. Step 1: Analyze traversal to last node

    The loop moves temp to last node, but does not track previous node.
  2. Step 2: Identify missing pointer update

    Deleting temp frees last node but previous node's next still points to freed memory, causing dangling pointer.
  3. Final Answer:

    Does not update previous node's next pointer to null -> Option C
  4. Quick Check:

    Previous node's next must be null after deletion [OK]
Hint: Track previous node to update next pointer before delete [OK]
Common Mistakes:
  • Not tracking previous node in traversal
  • Deleting node without updating links
  • Setting temp to nullptr does not fix list
5. You have a singly linked list with only one node. What should happen when you delete the last node?
hard
A. Set head pointer to null
B. Set node's next pointer to null
C. Delete node but keep head pointing to it
D. Do nothing, list remains same

Solution

  1. Step 1: Identify single node list condition

    Only one node means head points to that node and node's next is null.
  2. Step 2: Delete node and update head

    Deleting the only node requires setting head to null to mark empty list.
  3. Final Answer:

    Set head pointer to null -> Option A
  4. Quick Check:

    Single node deleted means empty list head = null [OK]
Hint: Single node deletion empties list, head = null [OK]
Common Mistakes:
  • Not setting head to null after deletion
  • Trying to update next pointer of deleted node
  • Leaving dangling pointer in head
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