3.1Singly Linked List
- 3.1.1What is a singly linked list?
- 3.1.2Creating a singly linked list
- 3.1.3Traversal
- 3.1.4Inserting at the end
- 3.1.5Inserting at a position
- 3.1.6Inserting at the head
- 3.1.7Deleting from the end
- 3.1.8Deleting from the head
- 3.1.9Deleting from the middle
- 3.1.10Searching for a value
- 3.1.11Reversing a linked list (iterative)
- 3.1.12Reversing a linked list (recursive)
Deleting from the end
There are three places to delete from in a linked list: the head, the end and the middle. This lesson covers removing the last node. The operation looks short, but it exposes a weak point of the singly linked list: a node does not know which node comes before it.
1Idea
To take the last node out of the list, the node before it must get NULL in its next field. A singly linked list has no link going backwards, so the only way to find that node is to traverse from the start.
The loop runs while temp->next->next != NULL. When the condition fails, temp is on the second-to-last node. In C the last node is returned with free, then temp->next is set to NULL.
Two cases are handled separately. If the list is empty there is nothing to delete. If the list has one node there is no second-to-last node; the node being deleted is also the first node and head becomes NULL. Because head can change, the C function takes struct Node**.
2Step by step
The drawing deletes the last node (9) of a four-node list holding 12, 7, 3 and 9. At each step the matching line of code lights up.
- Start. head points to 12. The node to delete is 9, but we do not know which node links to it.
tempstarts at the first node. The list is neither empty nor a single node, so the special cases are skipped.temp->next->nextpoints to 3, not NULL.tempmoves one node forward.- Now
temp->next->nextis NULL. The loop ends:tempis on the second-to-last node andtemp->nextis the last one. - In C the last node is returned with free. The next field of 3 still holds the old address. Java and Python have no such line; the garbage collector reclaims the node.
temp->nextis set to NULL. 3 is now the last node.- Result: 12 → 7 → 3. head did not change.
struct Node { int data; struct Node* next;}; void deleteTail(struct Node** head) { if (*head == NULL) return; // empty list if ((*head)->next == NULL) { // single node free(*head); *head = NULL; return; } struct Node* temp = *head; while (temp->next->next != NULL) // second to last temp = temp->next; free(temp->next); temp->next = NULL;}class Node { int data; Node next; Node(int data) { this.data = data; }} Node deleteTail(Node head) { if (head == null) return null; // empty list if (head.next == null) return null; // single node Node temp = head; while (temp.next.next != null) // second to last temp = temp.next; temp.next = null; return head;}class Node: def __init__(self, data): self.data = data self.next = None def delete_tail(head): if head is None or head.next is None: # empty or single node return None temp = head while temp.next.next is not None: # second to last temp = temp.next temp.next = None return headThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Reaching the second-to-last node means passing over n − 1 nodes.
Only one helper pointer (temp) is used.
Even with a tail pointer to the end of the list, deletion stays O(n): tail points to the last node, not the one before it. In a doubly linked list every node knows its predecessor, so deletion can be done in constant time. Deleting from the head is O(1) in every list: Deleting from the head.
4Common mistake
The most common mistake is skipping the one-node case:
struct Node* temp = *head;while (temp->next->next != NULL) // with one node, temp->next is NULL temp = temp->next;With a single node, temp->next is NULL and temp->next->next tries to read through a NULL pointer. The program crashes. Check for the empty list and the single node before the loop.
5Try it
What should happen when deleteTail is called on a list with only one node?
If we kept a tail pointer to the last node, how long would deleting from the end take?
The answer is B. Finding the node to delete is not enough; the next field of the node before it has to change. A singly linked list has no link to the previous node. When inserting at the end, on the other hand, tail does bring the cost down to O(1).
Show the answer
The answer is B. Finding the node to delete is not enough; the next field of the node before it has to change. A singly linked list has no link to the previous node. When inserting at the end, on the other hand, tail does bring the cost down to O(1).
Lessons you mark show up in the curriculum. This is kept on this device only.