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

# Insert before Kth node in doubly linked list

Data Structure and Algorithm · Chapter 4: Linked lists

- Asked in: 2072 Magh, 3 marks
- Video: [Insert A Node in the Doubly Linked List](https://www.youtube.com/watch?v=Jk8mrjXnBs8) — ProgrammingKnowledge
- 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-before-kth
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-4

## Question

Write an algorithm to insert an element before Kth node in doubly linked list.

## Short answer

Traverse to the Kth node (p). Allocate new node with info x; set new->next = p and new->prev = p->prev. If p->prev != NULL, set p->prev->next = new; else set start = new. Finally p->prev = new.

## Answer

Using the node `(prev, info, next)`, with `start` pointing to the first node and positions numbered from 1.

#### Algorithm INSERT_BEFORE_K(start, k, x)
1. Allocate a new node `new`; if NULL print "overflow" and stop. Set `new->info = x`.
2. Set `p = start`; repeat k-1 times: `p = p->next`. If `p` is NULL (empty list) or becomes NULL, print "invalid position" and stop. Now `p` is the K-th node.
3. Set `new->next = p`; `new->prev = p->prev`.
4. If `p->prev != NULL` then `p->prev->next = new`; else `start = new` (K was 1).
5. Set `p->prev = new`.

```text
Before: [a] <-> [p(K)]
After : [a] <-> [new] <-> [p(K)]
```
The order of the pointer assignments matters: copy `p->prev` into the new node before overwriting `p->prev`.

## Check yourself

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

1. To insert before the K-th node, to which node does p point?
   A. The (K-1)-th node
   B. The last node
   C. The K-th node
   D. The first node always
   Answer: C. p moves K-1 times from start; the new node goes before it. (Key point: Locate)

2. Why copy p->prev into new->prev before overwriting p->prev?
   A. To free memory
   B. To keep the list circular
   C. To update start
   D. Otherwise the original predecessor would be lost
   Answer: D. p->prev is replaced by new in the last step, so its old value is needed first. (Key point: Pointer order)

3. If p->prev is NULL, what is done?
   A. start = new
   B. p->prev->next = new
   C. new->next = NULL
   D. The insertion is refused
   Answer: A. K was 1, so the new node becomes the first node. (Key point: K=1 case)

## 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×
- [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
