Bird
Raised Fist0
DSA C++programming

Delete Node at End in DSA C++

Choose your learning style10 modes available
Mental Model
To remove the last item in a chain, find the second last item and cut off its link to the last one.
Analogy: Imagine a chain of paper clips linked together. To remove the last clip, you hold the second last clip and unhook the last one.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, delete node at end
Goal: Remove the last node so the list ends at the second last node
Step 1: start at head node 1
head -> [curr->1] -> 2 -> 3 -> null
Why: We begin from the start to find the node before the last
Step 2: move curr to node 2
head -> 1 -> [curr->2] -> 3 -> null
Why: We move forward to find the second last node
Step 3: check next node after curr (node 3) is last
head -> 1 -> [curr->2] -> 3 -> null
Why: When curr's next node's next is null, curr is second last
Step 4: set curr's next to null, removing last node
head -> 1 -> 2 -> null
Why: Cutting the link removes the last node from the list
Result:
head -> 1 -> 2 -> null
Annotated Code
DSA C++
#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class LinkedList {
public:
    Node* head;
    LinkedList() : head(nullptr) {}

    void append(int val) {
        if (!head) {
            head = new Node(val);
            return;
        }
        Node* curr = head;
        while (curr->next) {
            curr = curr->next;
        }
        curr->next = new Node(val);
    }

    void deleteAtEnd() {
        if (!head) return; // empty list
        if (!head->next) { // single node
            delete head;
            head = nullptr;
            return;
        }
        Node* curr = head;
        while (curr->next->next) {
            curr = curr->next; // advance to second last node
        }
        delete curr->next; // delete last node
        curr->next = nullptr; // cut link to last node
    }

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

int main() {
    LinkedList list;
    list.append(1);
    list.append(2);
    list.append(3);
    list.deleteAtEnd();
    list.printList();
    return 0;
}
if (!head) return; // empty list
handle empty list by returning early
if (!head->next) { delete head; head = nullptr; return; }
handle single node list by deleting head and setting to null
while (curr->next->next) { curr = curr->next; }
advance curr to second last node by checking next's next
delete curr->next; curr->next = nullptr;
delete last node and cut link from second last node
OutputSuccess
1 -> 2 -> null
Complexity Analysis
Time: O(n) because we traverse the list once to find the second last node
Space: O(1) because we use only a few pointers regardless of list size
vs Alternative: Compared to removing from the front which is O(1), deleting at end requires traversal making it O(n)
Edge Cases
empty list
function returns immediately without error
DSA C++
if (!head) return; // empty list
single node list
head node is deleted and list becomes empty
DSA C++
if (!head->next) { delete head; head = nullptr; return; }
When to Use This Pattern
When asked to remove the last item in a singly linked list, use traversal to find the second last node and cut its link.
Common Mistakes
Mistake: Trying to delete the last node without finding the second last node causes loss of access to the list.
Fix: Always find the node before the last to safely remove the last node.
Summary
Deletes the last node from a singly linked list.
Use when you need to remove the tail element from a linked list.
The key is to find the second last node and set its next pointer to null.

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