---
title: "Insert and delete kth node in doubly linked list"
subject: "Data Structure and Algorithm"
chapter: 4
key: "semester_4/data_structure_and_algorithm:doubly-insert-delete-kth"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/doubly-insert-delete-kth
site: BCT Engineering
---

# Insert and delete kth node in doubly linked list

Data Structure and Algorithm · Chapter 4: Linked lists

- Asked in: 2073 Bhadra, 5 marks; 2070 Bhadra, 5 marks (asked 2 times)
- Video: [Doubly Linked List | Insert, Delete, Complexity Analysis](https://www.youtube.com/watch?v=ZlNKNSz88Nk) — 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/doubly-insert-delete-kth
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-4

## Question

How do you insert and delete a node at the kth position of the doubly linked list?

## Short answer

Insert at kth: traverse to (k-1)th node, set new node's prev and next, link it between (k-1)th and kth; handle head insertion separately. Delete: find kth node, update its predecessor's next and successor's prev, free node; adjust head if needed. O(k) traversal.

## Answer

Let `start` point to the first node of a doubly linked list with nodes `(prev, info, next)`. Positions are numbered 1, 2, 3, ...

```c
struct dnode {
    struct dnode *prev;
    int info;
    struct dnode *next;
};
typedef struct dnode DNODE;
```

#### Insert a node at the k-th position (new node becomes the k-th node)
1. Allocate `new`; if NULL print "no memory" and stop. Set `new->info = x`.
2. If `k == 1`: `new->prev = NULL; new->next = start;` if `start != NULL` then `start->prev = new`; `start = new`; stop.
3. Set `p = start` and move `p = p->next` (k-2) times. If `p` is NULL at any point, print "position out of range" and stop. Now `p` is the (k-1)-th node.
4. `new->next = p->next; new->prev = p;`
5. If `p->next != NULL` then `p->next->prev = new`.
6. `p->next = new`.

```text
Before:  ... [p] <-> [q] ...
After :  ... [p] <-> [new] <-> [q] ...
new->prev = p, new->next = q, q->prev = new, p->next = new
```

#### Delete the node at the k-th position
1. If `start == NULL` print "underflow" and stop.
2. Set `p = start`; move `p = p->next` (k-1) times; if `p` becomes NULL print "position out of range" and stop.
3. If `p->prev != NULL` then `p->prev->next = p->next`; else `start = p->next` (deleting first node).
4. If `p->next != NULL` then `p->next->prev = p->prev`.
5. `x = p->info; free(p);`

```text
Before:  [a] <-> [p] <-> [b]
After :  [a] <-> [b]        a->next = b, b->prev = a, free(p)
```
Both operations take O(k) time for traversal; the pointer change itself is O(1). Special cases to remember: first node, last node, and a one-node list.

## Check yourself

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

1. When inserting at position 1 in a doubly linked list, what must be done to the old first node?
   A. Its prev is set to the new node, and start becomes the new node
   B. Its next is set to NULL
   C. It is freed
   D. Nothing needs to change
   Answer: A. The new first node must be linked both ways: new->next = start, start->prev = new. (Key point: Insert at k=1)

2. To insert as the k-th node, which node should p point to?
   A. The k-th node
   B. The (k-1)-th node
   C. The last node
   D. The (k+1)-th node
   Answer: B. The new node goes after the (k-1)-th node, so p moves k-2 times from start. (Key point: Locating predecessor)

3. After new->next = p->next and new->prev = p, which pointer updates complete the insertion?
   A. p->prev = new
   B. start = new
   C. p->next->prev = new (if not NULL), then p->next = new
   D. free(p->next)
   Answer: C. The old next node and p must both point to the new node; the NULL check handles insertion at the end. (Key point: Insert link order)

4. To delete node p in the middle, which two assignments are made?
   A. p->next = NULL and p->prev = NULL
   B. p->prev->prev = p->next
   C. start = p->next always
   D. p->prev->next = p->next and p->next->prev = p->prev
   Answer: D. Neighbours are linked to each other, bypassing p before it is freed. (Key point: Delete links)

## Related in Data Structure and Algorithm

- [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 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
- [Insert and delete before a node, singly linked](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Asingly-insert-delete-before) — 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 employee from doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-employee-delete) — 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
