---
title: "Insert and delete before a node, singly linked"
subject: "Data Structure and Algorithm"
chapter: 4
key: "semester_4/data_structure_and_algorithm:singly-insert-delete-before"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/singly-insert-delete-before
site: BCT Engineering
---

# Insert and delete before a node, singly linked

Data Structure and Algorithm · Chapter 4: Linked lists

- Asked in: 2070 Magh, 5 marks
- Video: [Singly Linked List | Insert, Delete, Complexity Analysis](https://www.youtube.com/watch?v=Ovhj6qDSF9M) — Blue Tree Code
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/singly-insert-delete-before
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-4

## Question

Write algorithms to insert and delete a node before a node in a singly linked list.

## Short answer

To insert before a key node: if key is the first node, make the new node the new start; otherwise, traverse with prev and cur to link the new node between pred and cur. To delete the node before a key: traverse with two trailing pointers to find the predecessor of the target, skip it, and free it. Both take O(n).

## Answer

Let `start` point to the first node of a singly linked list of nodes `(info, next)`, and let `key` be the info value of the given node (the one before which we operate).

#### Insert a new node before the node with value `key`
1. Allocate `new`; set `new->info = x`; if allocation fails stop.
2. If `start == NULL`, print "list empty" and stop.
3. If `start->info == key`: `new->next = start; start = new;` stop. (Insert before first node.)
4. Set `prev = start`, `cur = start->next`.
5. While `cur != NULL` and `cur->info != key`: `prev = cur; cur = cur->next`.
6. If `cur == NULL`, print "key not found" and stop.
7. `new->next = cur; prev->next = new;`

```text
Before: [prev] -> [cur(key)]
After : [prev] -> [new] -> [cur(key)]
```

#### Delete the node before the node with value `key`
1. If `start == NULL` or `start->info == key`, print "no node before key" and stop.
2. If `start->next != NULL` and `start->next->info == key`: the node before key is the first node: `t = start; start = start->next; free(t);` stop.
3. Set `prev = start`, `cur = start->next`.
4. While `cur != NULL` and `cur->next != NULL` and `cur->next->info != key`: `prev = cur; cur = cur->next`.
5. If `cur == NULL` or `cur->next == NULL`, print "key not found" and stop.
6. Now `cur` is the node before `key` node, `prev` is the node before `cur`: `prev->next = cur->next; free(cur);`

```text
Before: [prev] -> [cur] -> [key]
After : [prev] -> [key]          (cur freed)
```
Because a singly list has no `prev` pointer, we must keep trailing pointers while traversing, so both operations take O(n).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why are trailing pointers needed in a singly list?
   A. There is no prev pointer to reach the earlier node
   B. Nodes are contiguous
   C. To speed up malloc
   D. To avoid free()
   Answer: A. We can only move forward, so the previous node must be remembered. (Key point: Trailing pointer)

2. If start->info == key, how is the node inserted before it?
   A. prev->next = new
   B. new->next = start; start = new
   C. start->next = new
   D. new->next = NULL
   Answer: B. The key node is the first, so the head changes. (Key point: Insert before first)

3. In the general insert-before case, which links are set?
   A. cur->next = new
   B. prev->next = cur
   C. new->next = cur; prev->next = new
   D. start = new
   Answer: C. The new node sits between prev and cur (key node). (Key point: Insert links)

4. To delete the node before key, which statement unlinks it?
   A. cur->next = prev
   B. prev = NULL
   C. free(prev)
   D. prev->next = cur->next, then free(cur)
   Answer: D. cur is the node before key; its predecessor is linked to key. (Key point: Delete before)

## Related in Data Structure and Algorithm

- [Insert and delete kth node in doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-insert-delete-kth) — Data Structure and Algorithm, chapter 4, asked 2×
- [Insert before Kth node in doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-insert-before-kth) — Data Structure and Algorithm, chapter 4
- [Delete last node of doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adelete-end-node-doubly) — Data Structure and Algorithm, chapter 4
- [Delete Kth node from doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-delete-kth) — Data Structure and Algorithm, chapter 4
- [AVL delete then insert gives same tree?](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aavl-delete-insert-original) — Data Structure and Algorithm, chapter 6
- [Doubly vs singly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-vs-singly-linked) — Data Structure and Algorithm, chapter 4, asked 2×

## Related in other subjects

- [File system layout; i-node vs linked list](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afs-layout-inode-vs-linked-list) — Operating System, chapter 5
