A doubly linked list gives each node a pointer to both the next and the previous node, so it can be walked in both directions and a known node can be removed in O(1). In a circular linked list the last node points back to the first, so traversal loops. Both are variations of the singly linked list.
On this page
Types of linked list and which to useDoubly linked list operations in PythonCircular linked list operations in PythonApplied example: an LRU cacheInterview traps and what to practiseFrequently asked questionsThis page is the second step after the basic linked list: the two variants that exam papers and interviews ask for, with Python code that was run on Python 3.9.6 on 1 December 2026. It ends with an LRU cache, the problem where a doubly linked list earns its place.
If you have not yet compared the list with the array, read array vs linked list first.
Types of linked list and which to use
There are four types you will see named. A singly linked list has one pointer per node and one direction. A doubly linked list adds a pointer back. A circular list makes the end connect to the start, and can be singly or doubly linked. The fourth, a circular doubly linked list, combines both.
| Type | Pointers per node | Strength | Cost or limit | Used in |
|---|---|---|---|---|
| Singly | next | Smallest, simple | Cannot go back; deleting a known node needs its previous | Stacks, adjacency chains in hash tables |
| Doubly | next, prev | Both directions; O(1) delete of a known node | Extra pointer, more updates per change | LRU caches, editors, browser history, deque |
| Circular singly | next (last points to first) | Cycles without restarting; O(1) at both ends with a tail pointer | Loops forever if you forget the stop condition | Round-robin scheduling, ring buffers |
| Circular doubly | next, prev | Both directions in a ring | Most pointer updates | Playlists with repeat, carousels |
Complexities for the common operations follow from the pointer layout. Insert or delete at a position you already hold is O(1) in all of them. Reaching a position by index or searching by value is O(n) in all of them. Deleting a node given only the node itself is O(1) in a doubly linked list and O(n) in a singly linked one, because you need the previous node. For the wider cost model see time complexity and Big O.
Doubly linked list operations in Python
The node holds prev and next. The list keeps both head and tail, so adding at either end is O(1). The remove method receives a node, not a value, and fixes the two neighbours directly. This is the operation a singly linked list cannot do in constant time.
class DNode:
def __init__(self, val):
self.val, self.prev, self.next = val, None, None
class DoublyLinkedList:
def __init__(self):
self.head = self.tail = None
def append(self, val):
n = DNode(val)
if not self.tail:
self.head = self.tail = n
else:
n.prev, self.tail.next = self.tail, n
self.tail = n
def prepend(self, val):
n = DNode(val)
if not self.head:
self.head = self.tail = n
else:
n.next, self.head.prev = self.head, n
self.head = n
def remove(self, node): # O(1) when you already hold the node
if node.prev: node.prev.next = node.next
else: self.head = node.next
if node.next: node.next.prev = node.prev
else: self.tail = node.prev
def forward(self):
out, c = [], self.head
while c: out.append(c.val); c = c.next
return out
def backward(self):
out, c = [], self.tail
while c: out.append(c.val); c = c.prev
return out
d = DoublyLinkedList()
for x in [2, 3, 4]: d.append(x)
d.prepend(1)
print(d.forward(), d.backward())
d.remove(d.head.next.next) # remove the node holding 3, no search from the head
print(d.forward(), d.backward())[1, 2, 3, 4] [4, 3, 2, 1]
[1, 2, 4] [4, 2, 1]Walking forward gives 1, 2, 3, 4 and backward gives 4, 3, 2, 1. After removing the node holding 3, both directions agree again. The usual bug is updating next pointers and forgetting prev (or the reverse); always check the list in both directions after a change, as the output does here.
Handle four cases in remove: the node is the head, the tail, both (single node) or in the middle. The code covers them with the two if and else pairs. A pair of sentinel nodes (a fake head and a fake tail) removes those branches entirely, as the LRU cache below shows.
Circular linked list operations in Python
In a circular list no pointer is None. The trick that makes it practical is to keep only the tail pointer: tail.next is the head, so you reach both ends in O(1). Inserting at the front puts the new node after the tail without moving the tail pointer, and appending does the same and then moves the tail.
class Node:
def __init__(self, val):
self.val, self.next = val, None
class CircularLinkedList:
"""Keeps only a tail pointer; tail.next is the head, so both ends are O(1)."""
def __init__(self):
self.tail = None
def append(self, val):
n = Node(val)
if not self.tail:
n.next = n
self.tail = n
else:
n.next = self.tail.next
self.tail.next = n
self.tail = n
def prepend(self, val):
n = Node(val)
if not self.tail:
n.next = n
self.tail = n
else:
n.next = self.tail.next # same as append, but the tail pointer stays put
self.tail.next = n
def items(self, count):
out, cur = [], self.tail.next
for _ in range(count):
out.append(cur.val)
cur = cur.next
return out
c = CircularLinkedList()
for x in ["B", "C"]:
c.append(x)
c.prepend("A")
print(c.items(7)) # keeps going around: A B C A B C A['A', 'B', 'C', 'A', 'B', 'C', 'A']The items(7) call takes seven steps around a three-node ring and wraps without any special case. Because there is no None to stop on, every traversal needs a count or a check that you are back at the start. Forgetting this gives an infinite loop, the classic circular list bug.
Applied example: round-robin scheduling
Operating systems commonly give each process a short time slice and then move it to the back of the line. A ring models this: the pointer just advances. Here three processes need 5, 2 and 4 time units, and each turn gives 2 units.
# Round-robin scheduling: each process gets a time slice, then goes to the back
class Node:
def __init__(self, val):
self.val, self.next = val, None
def make_ring(vals):
nodes = [Node(v) for v in vals]
for a, b in zip(nodes, nodes[1:] + nodes[:1]):
a.next = b
return nodes[0]
cur = make_ring([("P1", 5), ("P2", 2), ("P3", 4)])
remaining = {"P1": 5, "P2": 2, "P3": 4}
quantum, order = 2, []
while remaining:
name = cur.val[0]
if name in remaining:
remaining[name] -= quantum
order.append(name)
if remaining[name] <= 0:
del remaining[name]
cur = cur.next
print(order)['P1', 'P2', 'P3', 'P1', 'P3', 'P1']P2 finishes in one turn, P3 in two, and P1 needs three. The order list shows who ran in each slice. In a real scheduler the finished node would be unlinked; the sketch skips finished ones for brevity. For a queue-flavoured view of the same idea, see the queue page.
Applied example: an LRU cache
A least recently used (LRU) cache has a fixed capacity. When it is full, it evicts the item that was used longest ago. The goal is O(1) for both get and put. Neither structure alone does it:
- A hash map finds a key in O(1) but keeps no usage order.
- A list keeps order but finding an item is O(n).
- Together: the hash map maps key to node, and a doubly linked list orders nodes from most to least recently used. A hit unlinks the node and moves it to the front, both O(1) because the map hands you the node and the doubly linked list can unlink it without searching.
class Node:
def __init__(self, key=None, val=None):
self.key, self.val, self.prev, self.next = key, val, None, None
class LRUCache:
def __init__(self, capacity):
self.cap, self.map = capacity, {}
self.head, self.tail = Node(), Node() # sentinels: most recent after head
self.head.next, self.tail.prev = self.tail, self.head
def _unlink(self, n):
n.prev.next, n.next.prev = n.next, n.prev
def _push_front(self, n):
n.next, n.prev = self.head.next, self.head
self.head.next.prev = n
self.head.next = n
def get(self, key):
if key not in self.map:
return -1
n = self.map[key]
self._unlink(n); self._push_front(n) # mark as most recently used
return n.val
def put(self, key, val):
if key in self.map:
self._unlink(self.map[key])
elif len(self.map) == self.cap:
lru = self.tail.prev # least recently used
self._unlink(lru)
del self.map[lru.key]
n = Node(key, val)
self.map[key] = n
self._push_front(n)
def order(self):
out, c = [], self.head.next
while c is not self.tail:
out.append(c.key); c = c.next
return out
c = LRUCache(2)
c.put("a", 1); c.put("b", 2)
print(c.get("a"), c.order())
c.put("c", 3) # evicts b, the least recently used
print(c.get("b"), c.order())
c.put("d", 4) # evicts a
print(c.get("a"), c.get("c"), c.order())1 ['a', 'b']
-1 ['c', 'a']
-1 3 ['c', 'd']Read the trace. After put a, put b, then get a, the order is a, b. Adding c evicts b, the least recently used, so get b returns -1. Adding d then evicts a. The two sentinel nodes (head and tail) mean the real nodes always have neighbours, so _unlink needs no if. The related hash structure is covered in hashmap explained.
In production Python you would normally use functools.lru_cache or an OrderedDict, which does the same bookkeeping internally. The shorter version below uses OrderedDict; interviewers still want the linked list version because it shows you understand why the operations are O(1).
from collections import OrderedDict
class LRU:
def __init__(self, cap):
self.cap, self.d = cap, OrderedDict()
def get(self, k):
if k not in self.d: return -1
self.d.move_to_end(k, last=False)
return self.d[k]
def put(self, k, v):
self.d[k] = v
self.d.move_to_end(k, last=False)
if len(self.d) > self.cap:
self.d.popitem(last=True)
c = LRU(2)
c.put("a", 1); c.put("b", 2); c.get("a"); c.put("c", 3)
print(list(c.d), c.get("b"))['c', 'a'] -1Interview traps and what to practise
- Updating
nextbut notprev(or the reverse) in a doubly linked list. - Not handling an empty list, a single node, the head and the tail separately, or not using sentinels.
- In a circular list, writing a
while node:loop that never ends. - Saying a doubly linked list is always better. It costs an extra pointer per node and more writes per change.
- Forgetting to delete the key from the hash map when evicting from an LRU cache.
Practise next: reverse a doubly linked list, flatten a multilevel doubly linked list, the Josephus problem on a circular list, and LFU cache as the harder cousin of LRU. Cycle detection with two pointers is shown in the array vs linked list page. A tree is the next structure built from nodes and pointers: tree data structure explained. The overall order of topics is on the data structures and algorithms learning path, with more questions in DSA interview questions.
Frequently asked questions
What is a doubly linked list?
A doubly linked list is a linked list where each node stores a value, a pointer to the next node and a pointer to the previous node. You can traverse it forwards and backwards, and remove a node you already hold in O(1) without searching from the head.
What is a circular linked list?
A circular linked list is one where the last node points back to the first instead of to null. It can be singly or doubly linked. Traversal loops, so you need a stop condition. It suits round-robin scheduling and repeating playlists.
What are the types of linked list?
The standard types are singly linked, doubly linked, circular singly linked and circular doubly linked. They differ in how many pointers each node holds and whether the end connects back to the start.
What is the advantage of a doubly linked list over a singly linked list?
It can move backwards and can delete a known node in O(1), because the node knows its predecessor. A singly linked list needs the previous node, found by walking from the head, which is O(n). The price is one extra pointer per node.
Why does an LRU cache use a doubly linked list and a hash map?
The hash map gives O(1) lookup from key to node. The doubly linked list keeps nodes in usage order and lets you unlink any node and move it to the front in O(1). Using either alone makes get or put O(n).
What are the basic operations on a linked list?
Insert (at the head, tail or after a node), delete (by node or value), search, traverse and reverse. Insert and delete are O(1) at a known position. Search and reaching the nth node are O(n).
Learn this with mentors
Programmes for builders and engineers with admissions open: live classes, mentors and project work.


