MCS-208 · Unit 3

Lists

Section 4 of 8

Block 1 · Unit 3Lists15 min · Section 4 of 8

3.4Insertion and Deletion in a Singly Linked List

Covers IGNOU MCS-208 §3.4

The big advantage of a linked list is that inserting or deleting a node only changes a few pointers; nothing is shifted. The difficulty is to handle every special case: empty list, first node, last node, key not found. This page gives the algorithms and a tested implementation.

Insertion

At the front (O(1)O(1)): the new node points to the old first node, and head points to the new node.

After a node p (O(1)O(1) once p is known):

  1. n->next = p->next; (new node points to p’s old successor)
  2. p->next = n; (p points to the new node)

The order matters: doing step 2 first would lose the address of the rest of the list.

Before the node holding key: if key is in the first node, insert at the front; otherwise walk with a pointer prev until prev->next holds key, then insert after prev. Finding the place is O(n)O(n).

Deletion

To delete the node holding key:

  1. If the list is empty, there is nothing to delete.
  2. If the first node holds key, set head to the second node and free the first.
  3. Otherwise find prev, the node before the one holding key. If there is none, report “not found”.
  4. Let victim = prev->next; set prev->next = victim->next (bypass it), then free(victim).

A singly linked node has no pointer to its predecessor, so we always search for the node before the one to change.

Insertion and deletion in a singly linked list

Cost of the operations

OperationSingly linked listArray list
Insert/delete at the frontO(1)O(1)O(n)O(n)
Insert after a node you already haveO(1)O(1)O(n)O(n)
Insert/delete by key (search first)O(n)O(n)O(n)O(n)
Access the ii-th elementO(i)O(i)O(1)O(1)
Insert at the end without a tail pointerO(n)O(n)O(1)O(1)

Key takeaways

  • Insert after p: n->next = p->next; p->next = n; (in this order).
  • Delete: find the node before the victim, bypass the victim, then free it.
  • Handle the empty list, the first node and “key not found” separately.
  • Pointer changes are O(1)O(1); searching for the position is O(n)O(n).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

p points to a node in the middle of a list and n is a new node. Which pair of statements inserts n after p correctly?

Show answer

Answer: B. n->next = p->next; p->next = n;

n must take over p’s old link before p is changed. In the first option n->next becomes n itself, and the rest of the list is lost.

Question 2 · multiple choice

What is the time to delete the first node of a singly linked list with nn nodes?

Show answer

Answer: A. O(1)O(1)

Only head changes; nothing is searched or shifted. Deleting the first element of an array list is O(n)O(n).

Question 3 · multiple choice

To delete the node holding key in a singly linked list, which node must the search find?

Show answer

Answer: C. The node just before the one holding key

Its next link has to change to bypass the deleted node, and a singly linked node cannot reach its predecessor. (A doubly linked list can.)

Question 4 · multiple choice

What does this program print?

C · q3-insert.c
#include <stdio.h>

typedef struct node {
    int data;
    struct node *next;
} Node;

int main(void)
{
    Node c = {3, NULL}, b = {2, &c}, a = {1, &b}, n = {9, NULL};
    Node *p = &b;
    n.next = p->next;
    p->next = &n;
    for (Node *q = &a; q != NULL; q = q->next)
        printf("%d ", q->data);
    printf("\n");
    return 0;
}
Show answer

Answer: D. 1 2 9 3

p is the node holding 2, so the new node 9 is linked in after it: 1 → 2 → 9 → 3. “1 9 2 3” would be insertion before 2.

Question 5 · multiple choice

A function deletes nodes from a list but never updates head when the first node is deleted. What happens on the next traversal after deleting the first node?

Show answer

Answer: D. head points to freed memory, so the behaviour is undefined.

The node was freed but head still holds its address (a dangling pointer). It may seem to work, print garbage or crash.

Question 6 · short answer

Write an algorithm to delete the node containing a given key from a singly linked list, covering all cases.

Show worked answer

(1) If head == NULL, report “list empty” and stop. (2) If head->data == key: temp = head; head = head->next; free(temp); stop. (3) Set prev = head. While prev->next != NULL and prev->next->data != key, set prev = prev->next. (4) If prev->next == NULL, report “key not found” and stop. (5) Otherwise victim = prev->next; prev->next = victim->next; free(victim);. Time: O(n)O(n) for the search, O(1)O(1) for the pointer changes.

↑ ↓ move↵ openTab switch type