Maintain Both Links in Doubly Linked Lists
Core idea
Each doubly linked node has next and prev references. For adjacent nodes a and b, a.next is b and b.prev is a. Head.prev and tail.next are None in this non-sentinel design. Given a known internal node, removal updates both neighbors in O(1); locating that node may still be linear.
class Node: def __init__(self, value): self.value = value self.prev = self.next = Nonea,b = Node('A'),Node('B')a.next = bb.prev = aprint(a.next.value, b.prev.value)print(a.next.prev is a)The reverse link agrees with the forward link.
Remove a middle node
class Node: def __init__(self, value): self.value = value self.prev = self.next = Nonea,b,c = Node(1),Node(2),Node(3)a.next,b.prev = b,ab.next,c.prev = c,bb.prev.next = b.nextb.next.prev = b.prevb.next = b.prev = Noneprint(a.next.value,c.prev.value)print(a.next.prev is a)Both neighbors bypass b. Its own links are cleared after unlinking.
Common mistake
This internal-node removal assumes both neighbors exist. For head or tail removal update the corresponding external reference and handle the singleton case.
Practice
Draw A–B–C before and after deleting B. Then specify the head, tail and singleton cases and verify both directional links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Doubly linked list
Check your understanding of doubly linked list.
What is printed?
class N: passa=N();b=N();a.next=b;b.prev=aprint(a.next.prev is a)What is printed?
class N: passa=N();a.prev=a.next=Noneprint(a.prev is a.next)What must middle-node removal update?
Cost of unlinking a known internal node?