What if removing the last item from your list could be as easy as snapping your fingers?
Why Delete Node at End in DSA C++?
Imagine you have a long paper chain made of connected rings. You want to remove the last ring, but you only have scissors that can cut one ring at a time, and you must find the second last ring first to safely remove the last one.
Manually finding the second last ring by counting or guessing is slow and can cause mistakes like cutting the wrong ring or breaking the chain. It becomes frustrating as the chain grows longer.
Using the "Delete Node at End" method in a linked list helps you quickly find and remove the last node by following the chain just once, making the process safe, fast, and error-free.
Node* temp = head;
while(temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;if(head == nullptr) return; if(head->next == nullptr) { delete head; head = nullptr; return; } Node* temp = head; while(temp->next->next != nullptr) { temp = temp->next; } delete temp->next; temp->next = nullptr;
This method enables efficient and safe removal of the last element in a linked list, keeping your data structure clean and up-to-date.
Think of a music playlist where you want to remove the last song you added. Using this method, you can quickly delete the last song without disturbing the rest of the playlist.
Manual removal of the last node is slow and error-prone.
Deleting the last node properly requires finding the second last node.
Using this method makes removal safe, fast, and reliable.
Practice
Solution
Step 1: Understand the linked list structure
Each node points to the next node. The last node points to null.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.Final Answer:
The second last node's next pointer becomes null -> Option AQuick Check:
Last node removed, previous node points to null [OK]
- Setting head to null incorrectly
- Deleting the first node instead
- Not updating the previous node's next pointer
Solution
Step 1: Identify the last node's pointer
In singly linked list, last node's previous node points to it via next pointer.Step 2: Delete last node and update pointer
Deleting lastNode->next and setting lastNode->next to nullptr correctly removes last node.Final Answer:
delete lastNode->next; lastNode->next = nullptr; -> Option DQuick Check:
Delete next node and set pointer null [OK]
- Deleting head instead of last node
- Using prev pointer in singly linked list
- Not setting pointer to nullptr after delete
Solution
Step 1: Identify the last node
The last node is 40, which points to null.Step 2: Remove last node and update previous node
Deleting 40 means 30's next pointer becomes null, ending list at 30.Final Answer:
10 -> 20 -> 30 -> null -> Option BQuick Check:
Last node 40 removed, list ends at 30 [OK]
- Not updating previous node's next pointer
- Deleting head node by mistake
- Outputting original list without deletion
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;
}Solution
Step 1: Analyze traversal to last node
The loop moves temp to last node, but does not track previous node.Step 2: Identify missing pointer update
Deleting temp frees last node but previous node's next still points to freed memory, causing dangling pointer.Final Answer:
Does not update previous node's next pointer to null -> Option CQuick Check:
Previous node's next must be null after deletion [OK]
- Not tracking previous node in traversal
- Deleting node without updating links
- Setting temp to nullptr does not fix list
Solution
Step 1: Identify single node list condition
Only one node means head points to that node and node's next is null.Step 2: Delete node and update head
Deleting the only node requires setting head to null to mark empty list.Final Answer:
Set head pointer to null -> Option AQuick Check:
Single node deleted means empty list head = null [OK]
- Not setting head to null after deletion
- Trying to update next pointer of deleted node
- Leaving dangling pointer in head
Ready to prove it?
Pass the test and this pattern moves unknown
