📖 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
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 A
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
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 D
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
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 B
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?