← Course home

Linear structures · Lesson 13 of 52

Doubly linked-list operations

Maintain both neighbor links during insertion, removal, and movement of known nodes.

Full lessonAbout 10 min

You will learn to update both neighbors when inserting or removing a node and explain why a known-node unlink is constant time.

Recommended first: Singly linked list.

On this page 6 sections

The idea

remove_node on the node for a, beside b, returns a and leaves b.

A doubly linked node stores prev and next. If you already hold the node, you do not walk from the head to find its neighbors. remove_node on the node for a, in a list that also holds b, returns a and leaves b. The unlink follows prev and next and rewires those neighbors to each other. insert_after splices a new node between a known node and that node's next. Both directions must be written. A missing reverse link makes a forward walk and a backward walk disagree. Finding a node by value is still linear, unless another structure stores the node. Sentinels are one way to make every real node have neighbors, including the empty list. This list keeps a head and a tail instead, and the endpoint cases update those caches.

A worked trace

StepStateWhat changed
Appendhead = aAppend a. Head and tail both hold a.
Appendtail = bAppend b. The tail is now b.
Unlinkremoved = aRemoving the known node a leaves b.

Why it is correct

The known-node claim is that the predecessor and the successor are fields, not search results. Rewiring them restores a single chain. The forward neighbor's prev and the backward neighbor's next must both change, or one direction still points at the removed node. insert_after writes the new node's two links and the two neighboring links that should point at it. That is four pointers when both neighbors exist. An endpoint has a missing neighbor, so the list updates head or tail instead of a second node. The size cache changes by one. After the edit, a walk from the head and a walk from the tail must list the same nodes in opposite orders.

A common mistake

Writing only the new node's links leaves the old neighbors pointing at each other.

What it costs

Unlinking or inserting beside a known node is a constant number of pointer writes. It is constant time. A singly linked delete of an arbitrary node is linear, because the predecessor must be found from the head. A search by value on this list is still linear. n such searches are quadratic if you repeat them. Extra memory is one node per item plus the list caches. Draw both directions after a removal. Test an empty list and a one-node list, and check that head and tail still agree. A known-node unlink is Θ(1) time and O(1) extra memory.

Run it

python
from bitsandbytes.doubly_linked_list import DoublyLinkedListchain = DoublyLinkedList()first = chain.append('a')chain.append('b')print(chain.remove_node(first))# prints: a 

Practice and next step

Open Stack basics next. Stack basics is the same idea of a single end, with the opposite arrival order.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.doubly_linked_list

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Unlink a known node

append a, then b. remove_node on the node for a returns a, and the list that remains is b.

head
a

State

  1. 0achosen

Append a. Head and tail both hold a.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1You insert a node between two existing neighbors. How many pointers must change?
2You already hold the node. What does remove_node cost?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.