DEV Community

Cover image for Circular and Doubly Linked Lists: Beyond the Basic Chain
Gouranga Das Samrat
Gouranga Das Samrat

Posted on

Circular and Doubly Linked Lists: Beyond the Basic Chain

When one direction isn't enough — and when the list needs to loop back on itself.


Recap: Limitations of Singly Linked Lists

The basic singly linked list is powerful, but it has two notable limitations:

  1. One-directional traversal — You can only move forward. To go back, you have to restart from the head.
  2. No loop-back — The last node is a dead end (points to null).

These limitations matter in certain use cases. Doubly linked lists solve problem #1. Circular linked lists solve problem #2.


Part 1: Doubly Linked Lists

What Is a Doubly Linked List?

In a doubly linked list, each node has two pointers:

  • next — points to the next node
  • prev — points to the previous node
null ← [10 | ↔] ↔ [20 | ↔] ↔ [30 | ↔] → null
        head                    tail
Enter fullscreen mode Exit fullscreen mode

This allows traversal in both directions.

Node Structure

class DoublyNode:
    def __init__(self, data):
        self.data = data
        self.next = None   # Points forward
        self.prev = None   # Points backward
Enter fullscreen mode Exit fullscreen mode

Doubly Linked List Class

class DoublyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
Enter fullscreen mode Exit fullscreen mode

Insertion in a Doubly Linked List

Insert at Head — O(1)

def insert_at_head(self, data):
    new_node = DoublyNode(data)
    if self.head is None:
        self.head = self.tail = new_node
        return
    new_node.next = self.head
    self.head.prev = new_node
    self.head = new_node
Enter fullscreen mode Exit fullscreen mode

Insert at Tail — O(1)

With a tail pointer, insertion at the end is also O(1):

def insert_at_tail(self, data):
    new_node = DoublyNode(data)
    if self.tail is None:
        self.head = self.tail = new_node
        return
    new_node.prev = self.tail
    self.tail.next = new_node
    self.tail = new_node
Enter fullscreen mode Exit fullscreen mode

Insert Before a Given Node — O(1) (once you have the node reference)

def insert_before(self, node, data):
    new_node = DoublyNode(data)
    new_node.next = node
    new_node.prev = node.prev
    if node.prev:
        node.prev.next = new_node
    else:
        self.head = new_node   # node was the head
    node.prev = new_node
Enter fullscreen mode Exit fullscreen mode

Deletion in a Doubly Linked List

Delete a Node — O(1) (once you have a direct reference to the node)

def delete_node(self, node):
    if node.prev:
        node.prev.next = node.next
    else:
        self.head = node.next   # Deleting head

    if node.next:
        node.next.prev = node.prev
    else:
        self.tail = node.prev   # Deleting tail
Enter fullscreen mode Exit fullscreen mode

This is a major advantage over singly linked lists — to delete a node in a singly linked list, you need its predecessor. In a doubly linked list, you can delete a node with just a reference to the node itself using its prev pointer.


Traversal: Forward and Backward

def traverse_forward(self):
    current = self.head
    while current:
        print(current.data, end=" → ")
        current = current.next

def traverse_backward(self):
    current = self.tail
    while current:
        print(current.data, end=" → ")
        current = current.prev
Enter fullscreen mode Exit fullscreen mode

Doubly vs. Singly Linked List

Property Singly Doubly
Memory per node data + 1 pointer data + 2 pointers
Traversal direction Forward only Both directions
Delete (with node ref) O(n) — need prev O(1) — has prev ptr
Insert before node O(n) O(1)
Complexity to implement Simple Moderate

Use a doubly linked list when:

  • You need backward traversal (e.g., browser back/forward).
  • You're frequently deleting nodes given a direct reference.
  • You're implementing a Deque (double-ended queue).
  • You're implementing an LRU cache (doubly linked list + hash map).

Part 2: Circular Linked Lists

What Is a Circular Linked List?

In a circular linked list, the last node points back to the first node instead of null. The list forms a loop.

Singly Circular:

[10] → [20] → [30] → [40]
  ↑_________________________|
Enter fullscreen mode Exit fullscreen mode

Doubly Circular:

↔ [10] ↔ [20] ↔ [30] ↔ [40] ↔
   ↑____________________________↓
Enter fullscreen mode Exit fullscreen mode

Key Difference: No Null Terminator

In a regular linked list, null marks the end. In a circular list, you must detect the loop by checking if you've returned to the starting node.

class CircularLinkedList:
    def __init__(self):
        self.head = None
Enter fullscreen mode Exit fullscreen mode

Insertion in Circular Linked List

Insert at Beginning — O(n)

def insert_at_beginning(self, data):
    new_node = Node(data)
    if self.head is None:
        self.head = new_node
        new_node.next = self.head   # Points to itself
        return

    # Find the last node
    current = self.head
    while current.next != self.head:
        current = current.next

    new_node.next = self.head
    current.next = new_node   # Last node now points to new_node
    self.head = new_node
Enter fullscreen mode Exit fullscreen mode

Insert at End — O(n)

def insert_at_end(self, data):
    new_node = Node(data)
    if self.head is None:
        self.head = new_node
        new_node.next = self.head
        return

    current = self.head
    while current.next != self.head:
        current = current.next

    current.next = new_node
    new_node.next = self.head   # Loop back to head
Enter fullscreen mode Exit fullscreen mode

Traversal in Circular Linked List

Because there's no null, you must stop when you circle back to the head:

def traverse(self):
    if self.head is None:
        return
    current = self.head
    while True:
        print(current.data)
        current = current.next
        if current == self.head:   # Stopped when we're back at start
            break
Enter fullscreen mode Exit fullscreen mode

Deletion in Circular Linked List

Delete Head Node — O(n)

def delete_head(self):
    if self.head is None:
        return
    if self.head.next == self.head:   # Only one node
        self.head = None
        return

    # Find last node to update its next pointer
    current = self.head
    while current.next != self.head:
        current = current.next

    current.next = self.head.next   # Last node now points to new head
    self.head = self.head.next
Enter fullscreen mode Exit fullscreen mode

Applications of Circular Linked Lists

Circular linked lists model circular or repeating systems naturally:

  • Round-robin scheduling — OS gives each process a time slice in a loop.
  • Multiplayer games — Player turns cycle around repeatedly.
  • Circular buffers / ring buffers — Audio streams, network packet buffers.
  • Josephus problem — Classic elimination problem solved efficiently with a circular list.
  • Media players — Repeat-all mode loops through the playlist.
  • Real-time clocks — Time wraps around (12 hours, 60 minutes).

Circular Doubly Linked List

Combining both concepts: each node has prev and next, and the list is circular.

head ↔ [10] ↔ [20] ↔ [30] ↔
  ↑_________________________________↓
Enter fullscreen mode Exit fullscreen mode

This is the structure used by Python's collections.deque internally and by many OS kernel implementations (Linux's list_head).

Benefits:

  • O(1) insertion and deletion at both ends.
  • O(1) deletion given a node reference.
  • Bidirectional traversal.
  • No null checks (the loop is always intact).

Summary Comparison

Feature Singly LL Doubly LL Circular LL Circular Doubly LL
Memory per node Low Medium Low Medium
Forward traversal ✓ ✓ ✓ ✓
Backward traversal ✗ ✓ ✗ ✓
Delete with node ref O(n) O(1) O(n) O(1)
No null end ✗ ✗ ✓ ✓
Complexity to implement Simple Moderate Moderate Complex

Summary

  • Doubly linked lists add a prev pointer to each node, enabling backward traversal and O(1) deletion given a node reference — at the cost of extra memory.
  • Circular linked lists connect the tail back to the head, forming a loop useful for round-robin and cycling applications.
  • Circular doubly linked lists combine both features for maximum flexibility — used in OS kernels and standard library implementations.
  • The right variant depends on your traversal needs, deletion patterns, and memory constraints.

Next up: Stacks — the beautifully simple LIFO structure built on top of linked lists (or arrays).


Top comments (0)