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 head
Deleting from the head is the reverse of inserting at the head and the shortest deletion in a linked list. Whatever the length of the list, only the first node and head are involved. This lesson also shows how to give the removed node's memory back correctly in C.
1Idea
The idea has three steps. Keep the address of the old first node in a pointer called temp. Move head to the second node, that is, to head->next. Finally, give the node that temp points to back with free.
In C, memory allocated with malloc is not returned on its own. If you do not call free for the node you took out, no pointer reaches it any more, yet it still takes up memory. This is a memory leak; the longer the program runs, the more memory it uses.
After free, temp still holds the old address. It is now a dangling pointer: reading or writing through it is undefined behaviour. That is why head->next is read before free.
Java and Python have no free. Moving head forward is enough; no reference to the old first node is left, so the garbage collector reclaims it. If the list is empty (head is NULL) there is nothing to delete and the function returns at once.
2Step by step
The drawing deletes the first node (12) of a three-node list holding 12, 7 and 3. At each step the matching line of code lights up.
- Start. head points to 12. This is the node to delete.
- The list is not empty. The address of the old first node is saved in
temp; once head changes,tempis the only way to reach that node. - head moves to the next node, 7. 12 is no longer part of the list, but
tempstill points to it. - In C the memory of 12 is returned with free. Java and Python have no such line; the garbage collector reclaims the node.
- Result: 7 → 3. The other nodes were not touched.
struct Node { int data; struct Node* next;}; void deleteHead(struct Node** head) { if (*head == NULL) return; // empty list struct Node* temp = *head; *head = (*head)->next; free(temp);}class Node { int data; Node next; Node(int data) { this.data = data; }} Node deleteHead(Node head) { if (head == null) return null; // empty list return head.next; // the new head}class Node: def __init__(self, data): self.data = data self.next = None def delete_head(head): if head is None: # empty list return None return head.next # the new headThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Independent of the length of the list: one comparison, two assignments and one free.
Only one helper pointer is used.
Compare: deleting from the end means you traverse the list to find the node before the last one, which makes it O(n). That is why a stack built on a linked list inserts and deletes at the head.
4Common mistake
The most common mistake is reading a node's field after freeing it:
free(*head);*head = (*head)->next; // reads from a freed nodeAfter the free call, *head is a dangling pointer and (*head)->next reads freed memory. The code sometimes seems to work and sometimes crashes. The right order: save it in temp, move head forward, call free last.
5Try it
What happens if we remove the free(temp) line from deleteHead in C?
The answer is B. head still moves forward, so the list looks right. But the memory of the old first node is never returned and can no longer be reached. That is a memory leak.
Show the answer
The answer is B. head still moves forward, so the list looks right. But the memory of the old first node is never returned and can no longer be reached. That is a memory leak.
What happens if deleteHead is called on an empty list?
The answer is A. Without the check, (*head)->next would try to read through a NULL pointer. That is why the empty-list case is handled on the first line of the function.
Show the answer
The answer is A. Without the check, (*head)->next would try to read through a NULL pointer. That is why the empty-list case is handled on the first line of the function.
Lessons you mark show up in the curriculum. This is kept on this device only.