Bird
Raised Fist0
DSA C++programming~10 mins

Delete Node at End in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete Node at End
Start at head
↓
Check if list empty?
Yes→Nothing to delete, EXIT
No↓
Traverse to second last node
↓
Remove last node
↓
Update second last node's next to null
↓
End
Start from the head, check if list is empty, then move to the second last node and remove the last node by updating pointers.
Execution Sample
DSA C++
struct Node {
  int data;
  Node* next;
};

void deleteEnd(Node*& head) {
  if (!head) return;
  if (!head->next) { delete head; head = nullptr; return; }
  Node* temp = head;
  while (temp->next->next) temp = temp->next;
  delete temp->next;
  temp->next = nullptr;
}
Deletes the last node from a singly linked list, handling empty and single-node lists.
Execution Table
StepActionCurrent Node DataNext Node DataPointer UpdateList State
1Check if head is nullhead=nullptr-No actionList empty, exit
2Check if head->next is nullhead=10next=nullptrNo actionSingle node list
3Delete head nodehead=10next=nullptrhead=nullptrList becomes empty
4Start traversal from headhead=10next=20No actionList: 10 -> 20 -> 30 -> null
5Move to next nodecurrent=10next=20No actionList unchanged
6Move to next nodecurrent=20next=30No actionList unchanged
7Check next->next is nullcurrent=20next=30No actionAt second last node
8Delete last node (30)current=20next=30Delete node 30List: 10 -> 20 -> null
9Update current->next to nullptrcurrent=20next=30current->next=nullptrList: 10 -> 20 -> null
10Endcurrent=20next=nullptrNo actionFinal list: 10 -> 20 -> null
💡 Execution stops when last node is deleted and second last node's next pointer is set to null.
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 8Final
head10->20->30->null10->20->30->null10->20->30->null10->20->30->null10->20->null
temp/currentnull10202020
Key Moments - 3 Insights
Why do we check if head->next is null before traversal?
Because if head->next is null, it means the list has only one node. We delete that node directly without traversal, as shown in execution_table row 2 and 3.
Why do we stop traversal at the second last node?
We stop at the second last node because we need to delete the last node and update the second last node's next pointer to null. This is shown in rows 6 to 9.
What happens if the list is empty when deleteEnd is called?
The function returns immediately without any action, as shown in execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'temp' after step 6?
ANode with data 20
BNode with data 10
CNode with data 30
Dnullptr
💡 Hint
Check the 'temp/current' row in variable_tracker after step 6.
At which step does the last node get deleted?
AStep 7
BStep 9
CStep 8
DStep 10
💡 Hint
Look at the 'Action' column in execution_table where 'Delete last node (30)' happens.
If the list had only one node, which step would delete it?
AStep 5
BStep 3
CStep 8
DStep 10
💡 Hint
Refer to execution_table rows 2 and 3 for single node list deletion.
Concept Snapshot
Delete Node at End in singly linked list:
- Check if list empty: do nothing
- If single node: delete head, set head=nullptr
- Else traverse to second last node
- Delete last node
- Set second last node's next to nullptr
- Time: O(n), Space: O(1)
Full Transcript
To delete the last node in a singly linked list, first check if the list is empty. If empty, do nothing. If the list has only one node, delete it and set head to null. Otherwise, start from the head and move through the list until you reach the second last node. Then delete the last node and update the second last node's next pointer to null. This process ensures the list remains connected properly after deletion. The code handles all cases safely and runs in linear time proportional to the list length.

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