---
title: "Delete last node of doubly linked list"
subject: "Data Structure and Algorithm"
chapter: 4
key: "semester_4/data_structure_and_algorithm:delete-end-node-doubly"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/delete-end-node-doubly
site: BCT Engineering
---

# Delete last node of doubly linked list

Data Structure and Algorithm · Chapter 4: Linked lists

- Asked in: 2074 Bhadra, 5 marks
- Video: [Delete a node from Doubly Linked List(start / middle/ end node)](https://www.youtube.com/watch?v=LvUgew66zOQ) — Vivekanand Khyade - Algorithm Every Day
- 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/delete-end-node-doubly
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-4

## Question

How do you delete a node at the end of the doubly linked list?

## Short answer

Move a pointer p to the last node by following next until p->next is NULL. If p->prev is NULL (only one node), set start to NULL; else set p->prev->next = NULL. Store p->info, free(p), return the value. Finding the last node takes O(n) unless a rear pointer is used.

## Answer

Let `start` point to the first node of a doubly linked list.

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

#### Algorithm
1. If `start == NULL`, print "underflow (list empty)" and stop.
2. Set `p = start`; while `p->next != NULL`, set `p = p->next`. Now `p` is the last node.
3. If `p->prev == NULL` (only one node): set `start = NULL`.
   Else: set `p->prev->next = NULL` (second-last node becomes the last).
4. `x = p->info; free(p);` return x.

```text
Before:  start -> [a] <-> [b] <-> [p]
After :  start -> [a] <-> [b] -> NULL        (p freed)
         b->next = NULL
```

```c
int delete_end(DNODE **start) {
    DNODE *p = *start; int x;
    if (p == NULL) { printf("Underflow\n"); return -1; }
    while (p->next != NULL) p = p->next;        /* go to last node */
    if (p->prev == NULL) *start = NULL;         /* only one node */
    else p->prev->next = NULL;
    x = p->info;
    free(p);
    return x;
}
```
Finding the last node takes O(n); the `prev` pointer removes the need for a trailing pointer that a singly list requires. If a `rear` pointer is kept, deletion is O(1).

## Check yourself

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

1. How is the last node found in the algorithm?
   A. Use start->prev
   B. Move p along next until p->next is NULL
   C. Move p back along prev
   D. It is always at index n-1
   Answer: B. Starting at start, follow next to the end; the prev pointer is not needed to locate it. (Key point: Finding last)

2. If p->prev is NULL for the last node, what is done?
   A. p->prev->next is set to NULL
   B. p is not freed
   C. start is set to NULL because it was the only node
   D. start is set to p->next
   Answer: C. Only the first node has a NULL prev; if it is also last, the list becomes empty. (Key point: Single node case)

3. Otherwise, which assignment removes the last node from the list?
   A. p->next = p->prev
   B. start->next = NULL
   C. p->prev = NULL
   D. p->prev->next = NULL
   Answer: D. The second-last node becomes the new last node, ending in NULL. (Key point: Else 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
- [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 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
- [Doubly linked list: bank customer records](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-bank-customer) — Data Structure and Algorithm, chapter 4

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