Bird
Raised Fist0
DSA C++programming~30 mins

Delete Node at End in DSA C++ - Build from Scratch

Choose your learning style10 modes available
Delete Node at End in a Singly Linked List
📖 Scenario: You are managing a simple list of tasks using a singly linked list. Sometimes, you need to remove the last task from the list when it is completed.
🎯 Goal: Build a singly linked list with 3 nodes, then delete the last node, and finally print the list to see the updated tasks.
📋 What You'll Learn
Create a singly linked list with exactly 3 nodes containing values 10, 20, and 30
Create a pointer called head pointing to the first node
Write a function called deleteAtEnd that deletes the last node of the list
Print the list after deletion in the format: 10 -> 20 -> null
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like task scheduling, undo functionality in editors, and managing playlists.
💼 Career
Understanding linked list operations like deletion is fundamental for software development roles involving data structures and memory management.
Progress0 / 4 steps
1
Create a singly linked list with 3 nodes
Create a struct called Node with an int data and a Node* next. Then create 3 nodes with values 10, 20, and 30. Link them so that head points to the first node.
DSA C++
Hint

Start by defining the Node structure. Then create each node with the given values and link them by setting the next pointers.

2
Create the deleteAtEnd function
Write a function called deleteAtEnd that takes Node*& head and deletes the last node of the list. Handle the case when the list is empty or has only one node.
DSA C++
Hint

Check if the list is empty or has one node. If more, loop to the second last node and delete the last node.

3
Call deleteAtEnd to remove the last node
Call the function deleteAtEnd with the variable head to delete the last node from the list.
DSA C++
Hint

Simply call deleteAtEnd with head as argument to remove the last node.

4
Print the linked list after deletion
Write a loop to print the linked list starting from head. Print each node's data followed by ->. End with null.
DSA C++
Hint

Use a while loop to go through each node starting from head. Print the data and arrow until the end, then print 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