📖 Scenario: Imagine you have a chain of boxes connected one after another. Each box holds a number and points to the next box. This is like a singly linked list. Sometimes, you want to remove the last box from the chain.
🎯 Goal: You will create a singly linked list with 3 nodes, then write code to remove the last node from the list, and finally print the list to see the result.
📋 What You'll Learn
Create a singly linked list with exactly 3 nodes containing values 10, 20, and 30 in that order
Create a pointer variable called head pointing to the first node
Write a function called deleteAtEnd that removes the last node from the list
Print the list after deletion showing the nodes connected with arrows like: 10 -> 20 -> NULL
💡 Why This Matters
🌍 Real World
Linked lists are used in many software systems to manage collections of items where size changes often, like music playlists or undo history.
💼 Career
Understanding how to manipulate linked lists is fundamental for software developers working with data structures, memory management, and system programming.
Progress0 / 4 steps
1
Create the singly linked list with 3 nodes
Create a struct called Node with an int data and a struct 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
Remember to use malloc to create nodes and link them by setting the next pointer.
2
Create the deleteAtEnd function
Write a function called deleteAtEnd that takes a pointer to Node* and removes the last node from the list. Handle the case when the list has only one node.
DSA C
Hint
Use two pointers to find the second last node. Free the last node and set second last node's next to NULL.
3
Print the linked list
Write a function called printList that takes Node* head and prints the list nodes in the format: 10 -> 20 -> NULL. Then call printList(head) in main after deleting the last node.
DSA C
Hint
Use a loop to print each node's data followed by '->'. After the loop, print 'NULL'.
4
Print the final linked list after deletion
Run the program and observe the printed output after deleting the last node. The output should be: 10 -> 20 -> NULL
DSA C
Hint
Make sure you call printList(head) after deleting the last node to see the updated list.
Practice
(1/5)
1. What is the main purpose of the Delete Node at End operation in a singly linked list?
easy
A. To reverse the linked list
B. To add a new node at the end of the linked list
C. To delete the first node of the linked list
D. To remove the last node from the linked list
Solution
Step 1: Understand the operation's goal
The operation aims to remove the last node in the linked list, which is the node that points to null.
Step 2: Identify the correct description
Removing the last node means deleting the node at the end, not adding or reversing nodes.
Final Answer:
To remove the last node from the linked list -> Option D
Quick Check:
Delete Node at End = Remove last node [OK]
Hint: Deleting at end means removing the last node [OK]
Common Mistakes:
Confusing delete at end with delete at beginning
Thinking it adds a node instead of deleting
Mixing up with reversing the list
2. Which of the following C code snippets correctly deletes the last node of a singly linked list named head?
easy
A. if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL;
B. Node* temp = head; while (temp->next != NULL) temp = temp->next; free(temp);
C. free(head); head = NULL;
D. Node* temp = head; while (temp->next != NULL) temp = temp->next; temp->next = NULL;
Solution
Step 1: Check for empty or single-node list and traverse to second last node
if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; checks if list is empty or single node, then moves to the node before last using temp->next->next != NULL.
Step 2: Free last node and update pointer
It frees the last node and sets temp->next to NULL, correctly deleting the last node.
Final Answer:
if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; -> Option A
Quick Check:
Traverse to second last, free last, set next NULL = if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; [OK]
Hint: Stop at second last node before deleting last [OK]
Common Mistakes:
Freeing node without updating previous node's next pointer
Not checking for empty list
Freeing head without traversal
3. What will be the output linked list after deleting the last node from the list: 10 -> 20 -> 30 -> 40 -> NULL using the standard delete at end operation?
medium
A. 10 -> 20 -> 30 -> 40 -> NULL
B. 10 -> 20 -> 30 -> NULL
C. 20 -> 30 -> 40 -> NULL
D. 10 -> 20 -> NULL
Solution
Step 1: Identify the last node
The last node is 40, which points to NULL.
Step 2: Remove the last node and update the second last node's next pointer
After deletion, 30 becomes the last node and its next pointer is set to NULL.
A. It does not update the second last node's next pointer to NULL
B. It frees the head node instead of the last node
C. It causes a segmentation fault by accessing NULL pointer
D. It deletes the first node instead of the last
Solution
Step 1: Analyze traversal logic
The loop moves temp to the last node (where temp->next is NULL).
Step 2: Check pointer update after freeing
It frees the last node but does not update the second last node's next pointer to NULL, leaving a dangling pointer.
Final Answer:
It does not update the second last node's next pointer to NULL -> Option A
Quick Check:
Missing next pointer update causes dangling pointer [OK]
Hint: Always update second last node's next to NULL after free [OK]
Common Mistakes:
Freeing node without updating previous node's next
Not handling empty list
Incorrect loop condition causing crash
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> 35 -> 45 -> NULL. Write the steps to delete the last node and then insert a new node with value 50 at the end. What will be the final list?
hard
A. 5 -> 15 -> 25 -> 35 -> NULL
B. 5 -> 15 -> 25 -> 35 -> 45 -> 50 -> NULL
C. 5 -> 15 -> 25 -> 35 -> 50 -> NULL
D. 50 -> 5 -> 15 -> 25 -> 35 -> NULL
Solution
Step 1: Delete the last node (45)
Traverse to second last node (35), free last node (45), set 35's next to NULL.
Step 2: Insert new node with value 50 at end
Traverse to last node (35), create new node with 50, set 35's next to new node, new node's next to NULL.
Final Answer:
5 -> 15 -> 25 -> 35 -> 50 -> NULL -> Option C
Quick Check:
Delete last, then add 50 at end = 5 -> 15 -> 25 -> 35 -> 50 -> NULL [OK]
Hint: Delete last node, then add new node at end [OK]
Common Mistakes:
Not updating pointers correctly after deletion
Appending new node without deleting last
Inserting new node at wrong position
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.