algoritmauzmanı
TREN
3.1Singly Linked List
Lesson 3.1.7

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 . 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.

What you will learn
  • Why deleting the last node means finding the second-to-last node first
  • Why the empty list and the one-node list need their own cases
  • Why the operation is and why a pointer does not fix that
6 minTime O(n)Memory O(1)C · Java · Python

1Idea

To take the last node out of the list, the node before it must get in its field. A singly linked list has no link going backwards, so the only way to find that node is to 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 , then temp->next is set to .

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 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.

NULL12?0x1a407?0x1a903?0x1b209?0x1c60headtemp
  1. Start. points to 12. The node to delete is 9, but we do not know which node links to it.
  2. temp starts at the first node. The list is neither empty nor a single node, so the special cases are skipped.
  3. temp->next->next points to 3, not . temp moves one node forward.
  4. Now temp->next->next is . The loop ends: temp is on the second-to-last node and temp->next is the last one.
  5. In C the last node is returned with . The field of 3 still holds the old address. Java and Python have no such line; the garbage collector reclaims the node.
  6. temp->next is set to . 3 is now the last node.
  7. Result: 12 → 7 → 3. 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 head

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

3Complexity

Time
O(n)

Reaching the second-to-last node means passing over n − 1 nodes.

Memory
O(1)

Only one helper pointer (temp) is used.

Even with a pointer to the end of the list, deletion stays : 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 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 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?

Show the answer

The answer is B. A single node is both the first and the last node. Removing it empties the list, so must be set to . That is why the C function takes struct Node**.

If we kept a tail pointer to the last node, how long would deleting from the end take?

Show the answer

The answer is B. Finding the node to delete is not enough; the 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, does bring the cost down to .

Lessons you mark show up in the curriculum. This is kept on this device only.