The basic operations of a singly linked list work by following and changing pointers. Care is needed so that no node is lost.

Traversing and Searching

To traverse the list, a pointer p starts at HEAD and moves with p = p->next until p is NULL, processing each node on the way. Counting nodes, printing and summing all use traversal, in O(n) time. To search for a value, we traverse and compare each data field with the key, stopping when it is found or the list ends. Since there is no direct access, binary search is not practical, and searching is O(n).

Definition: Traversal. The process of visiting each node of a linked list exactly once, by following the next pointers from the head to the end.

Insertion

To insert at the beginning, create a new node, set its next to HEAD, and set HEAD to the new node. This takes O(1). To insert at the end, traverse to the last node and set its next to the new node, which takes O(n), or O(1) if a TAIL pointer is kept. To insert after a given position or node, traverse to that node p, set new->next = p->next, and then p->next = new. The order of these two steps matters: if p->next is changed first, the rest of the list is lost.

Deletion

To delete the first node, save HEAD in a temporary pointer, move HEAD to HEAD->next, and free the old node. To delete the last node, traverse to the second-last node, set its next to NULL and free the last node. To delete a node with a given value, traverse keeping a pointer to the previous node, and when the value is found, set prev->next = curr->next and free curr. Special cases are an empty list (underflow), a list with one node, and deleting the head itself.

Table 2.2: Cost of singly linked list operations

Operation Time Note
Insert at beginning O(1) Change HEAD
Insert at end O(n) O(1) with a TAIL pointer
Delete first O(1) Move HEAD
Search / delete by value O(n) Traverse

Example 2.2: Insertion and deletion

Problem: Start with the list 20 → 30. Insert 10 at the beginning, insert 40 at the end, then delete the node with value 30.

Solution:

  1. Insert 10 at the beginning: 10 → 20 → 30.
  2. Insert 40 at the end: 10 → 20 → 30 → 40.
  3. Delete 30: the node 20 now points to 40, giving 10 → 20 → 40.

Result: The list becomes 10 → 20 → 40.

#include <stdio.h>
#include <stdlib.h>
struct node { int data; struct node *next; };
struct node *head = NULL;
void push_front(int v) {
    struct node *n = malloc(sizeof *n);
    n->data = v; n->next = head; head = n;
}
void push_back(int v) {
    struct node *n = malloc(sizeof *n), *p = head;
    n->data = v; n->next = NULL;
    if (!head) { head = n; return; }
    while (p->next) p = p->next;
    p->next = n;
}
void remove_value(int v) {
    struct node *prev = NULL, *cur = head;
    while (cur && cur->data != v) { prev = cur; cur = cur->next; }
    if (!cur) return;
    if (prev) prev->next = cur->next; else head = cur->next;
    free(cur);
}
void show(void) {
    for (struct node *p = head; p; p = p->next)
        printf("%d ", p->data);
    printf("\n");
}
int main(void) {
    push_back(20); push_back(30);
    push_front(10); push_back(40); show();
    remove_value(30); show();
    return 0;
}

Output

10 20 30 40
10 20 40

Common mistakes

  • Setting p->next before new->next, which cuts off the rest of the list.
  • Not handling the empty list when deleting.
  • Forgetting to update HEAD when the first node is deleted.

Key takeaways

  • Traverse with p = p->next until p is NULL.
  • Insert after p: new->next = p->next, then p->next = new.
  • Delete by value: keep a pointer to the previous node.
  • Insertion and deletion at the head are O(1); search is O(n).

Practice questions

  1. How is a node inserted at the beginning?
  2. Why is a previous pointer needed when deleting by value?
  3. What is underflow in a linked list?

Answers to the practice questions

  1. Set new->next = HEAD, then HEAD = new.
  2. To link the previous node to the node after the deleted one.
  3. An attempt to delete from an empty list.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit II