Skip to main content

Delete Kth node from doubly linked list

Short answer

If list empty, underflow. Traverse to Kth node p. If p->prev != NULL, set p->prev->next = p->next else start = p->next. If p->next != NULL, set p->next->prev = p->prev. Free p and return its info.

Write an algorithm for deleting Kth node from the doubly linked list.

With node (prev, info, next) and start pointing to the first node:

Algorithm DELETE_K(start, k)

  1. If start == NULL print "underflow" and stop.
  2. Set p = start; repeat k-1 times: p = p->next; if p becomes NULL print "invalid position" and stop.
  3. If p->prev != NULL then p->prev->next = p->next; else start = p->next.
  4. If p->next != NULL then p->next->prev = p->prev.
  5. x = p->info; free(p); return x.
Before: [a] <-> [p(K)] <-> [b]
After : [a] <-> [b]

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 4 questions →