algoritmauzmanı
TREN
3.1Singly Linked List
Lesson 3.1.8

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 and are involved. This lesson also shows how to give the removed node's memory back correctly in C.

What you will learn
  • Why the old head is kept in a temporary pointer while the first node is deleted
  • What happens if is never called, and what happens if it is called in the wrong order
  • Why the operation is , and the empty-list case
4 minTime O(1)Memory O(1)C · Java · Python

1Idea

The idea has three steps. Keep the address of the old first node in a called temp. Move to the second node, that is, to head->next. Finally, give the node that temp points to back with .

In C, memory allocated with 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 ; the longer the program runs, the more memory it uses.

After free, temp still holds the old address. It is now a : reading or writing through it is undefined behaviour. That is why head->next is read before free.

Java and Python have no free. Moving forward is enough; no to the old first node is left, so the garbage collector reclaims it. If the list is empty (head is ) 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.

NULL12?0x1a407?0x1a903?0x1b20headtemp
  1. Start. points to 12. This is the node to delete.
  2. The list is not empty. The address of the old first node is saved in temp; once changes, temp is the only way to reach that node.
  3. moves to the next node, 7. 12 is no longer part of the list, but temp still points to it.
  4. In C the memory of 12 is returned with . Java and Python have no such line; the garbage collector reclaims the node.
  5. 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 head

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

Independent of the length of the list: one comparison, two assignments and one free.

Memory
O(1)

Only one helper pointer is used.

Compare: deleting from the end means you the list to find the node before the last one, which makes it . 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 node

After the free call, *head is a 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?

Show the answer

The answer is B. 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 .

What happens if deleteHead is called on an empty list?

Show the answer

The answer is A. Without the check, (*head)->next would try to read through a 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.