Doubly Linked List in Python
Last Updated :
23 Jul, 2025
Doubly Linked List is a type of linked list in which each node contains a data element and two links pointing to the next and previous node in the sequence. This allows for more efficient operations such as traversals, insertions, and deletions because it can be done in both directions.

Doubly Linked List (DLL) is a special type of linked list in which each node contains a pointer to the previous node as well as the next node of the linked list. In a Doubly Linked List, we can traverse in forward and backward direction using the next and previous pointer respectively.
Representation of Doubly Linked List in Python:
Here is the representation of the doubly linked list in python:
Python
# Node of a doubly linked list
class Node:
def __init__(self, next=None, prev=None, data=None):
# reference to next node in DLL
self.next = next
# reference to previous node in DLL
self.prev = prev
self.data = data
Traversal of Doubly Linked List in Python:
To traverse a doubly linked list in Python, you can simply start from the head of the list and iterate through each node, printing its data.
Below is the implementation of the above idea:
Python
# Python Program for traversal of a doubly linked list
class Node:
def __init__(self, data):
# Initialize a new node with data, previous, and next pointers
self.data = data
self.next = None
self.prev = None
def traverse(head):
# Traverse the doubly linked list and print its elements
current = head
while current:
# Print current node's data
print(current.data, end=" <-> ")
# Move to the next node
current = current.next
print("None")
def insert_at_beginning(head, data):
# Insert a new node at the beginning of the doubly linked list
new_node = Node(data)
new_node.next = head
if head:
head.prev = new_node
return new_node
# Driver Code
head = None
head = insert_at_beginning(head, 4)
head = insert_at_beginning(head, 3)
head = insert_at_beginning(head, 2)
head = insert_at_beginning(head, 1)
# To traverse and print the nodes:
traverse(head)
Output1 <-> 2 <-> 3 <-> 4 <-> None
Insertion of Doubly Linked List in Python:
Inserting a new node in a doubly linked list is very similar to inserting new node in linked list. There is a little extra work required to maintain the link of the previous node. A node can be inserted in a Doubly Linked List in five ways:
- At the front of the DLL.
- After a given node.
- Before a given node.
- At the end of the DLL.
1. Insertion at the Beginning:
To insert a node at the beginning of a doubly linked list in Python, you need to follow these steps:
- Create a new node with the given data.
- Set the "next" pointer of the new node to point to the current head (if any).
- Set the "previous" pointer of the new node to None (as it will become the new head).
- If the list is not empty, update the "previous" pointer of the current head to point to the new node.
- Update the head of the list to point to the new node.
Below is the implementation of the above idea:
Python
# Python Program for a doubly linked list at the beginning of a node
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Function to insert a node at the beginning of a doubly linked list
def insert_at_beginning(head, data):
new_node = Node(data)
new_node.next = head
if head:
head.prev = new_node
return new_node
# Function to display the elements of the doubly linked list
def display(head):
current = head
while current:
print(current.data, end=" <-> ")
current = current.next
print("None")
# Driver Code
head = None
head = insert_at_beginning(head, 3)
head = insert_at_beginning(head, 2)
head = insert_at_beginning(head, 1)
print("Doubly Linked List after insertion at the beginning:")
display(head)
OutputDoubly Linked List after insertion at the beginning:
1 <-> 2 <-> 3 <-> None
2. Insertion after a given node:
To insert a node after a given node in a doubly linked list in Python, you can follow these steps:
- Create a new node with the given data.
- Set the "next" pointer of the new node to point to the next node of the given node.
- Set the "previous" pointer of the new node to point to the given node.
- If the next node of the given node is not None, update the "previous" pointer of that node to point to the new node.
- Update the "next" pointer of the given node to point to the new node.
Below is the implementation of the above idea:
Python
# Python Program for Insertion after a given node
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Function to insert a node after a given node in a doubly linked list
def insert_after_node(node, data):
if node is None:
print("Error: The given node is None")
return
new_node = Node(data)
new_node.prev = node
new_node.next = node.next
if node.next:
node.next.prev = new_node
node.next = new_node
# Function to display the elements of the doubly linked list
def display(head):
current = head
while current:
print(current.data, end=" <-> ")
current = current.next
print("None")
# Driver Code
head = Node(1)
node2 = Node(2)
node3 = Node(3)
head.next = node2
node2.prev = head
node2.next = node3
node3.prev = node2
print("Doubly Linked List before insertion:")
display(head)
insert_after_node(node2, 4)
print("Doubly Linked List after insertion:")
display(head)
OutputDoubly Linked List before insertion:
1 <-> 2 <-> 3 <-> None
Doubly Linked List after insertion:
1 <-> 2 <-> 4 <-> 3 <-> None
3. Insertion before a given node:
To insert a node before a given node in a doubly linked list in Python, you can follow these steps:
- Create a new node with the given data.
- Set the "next" pointer of the new node to point to the given node.
- Set the "previous" pointer of the new node to point to the previous node of the given node.
- If the previous node of the given node is not None, update the "next" pointer of that node to point to the new node.
- Update the "previous" pointer of the given node to point to the new node.
Below is the implementation of the above idea:
Python
# Python Program for Insertion before a given node
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Function to insert a node before a given node in a doubly linked list
def insert_before_node(node, data):
if node is None:
print("Error: The given node is None")
return
new_node = Node(data)
new_node.prev = node.prev
new_node.next = node
if node.prev:
node.prev.next = new_node
node.prev = new_node
# Function to display the elements of the doubly linked list
def display(head):
current = head
while current:
print(current.data, end=" <-> ")
current = current.next
print("None")
# Driver Code
head = Node(1)
node2 = Node(2)
node3 = Node(3)
head.next = node2
node2.prev = head
node2.next = node3
node3.prev = node2
print("Doubly Linked List before insertion:")
display(head)
insert_before_node(node2, 4)
print("Doubly Linked List after insertion:")
display(head)
OutputDoubly Linked List before insertion:
1 <-> 2 <-> 3 <-> None
Doubly Linked List after insertion:
1 <-> 4 <-> 2 <-> 3 <-> None
4. Insertion at the end:
To insert a node at the end of a doubly linked list in Python, you need to follow these steps:
- Create a new node with the given data.
- If the list is empty (head is None), make the new node the head of the list.
- Otherwise, traverse the list to find the last node.
- Set the "next" pointer of the last node to point to the new node.
- Set the "previous" pointer of the new node to point to the last node.
- Optionally, update the head of the list to point to the new node if it's the first node in the list.
Below is the implementation of the above idea:
Python
# Python Program for Insertion at the end
class Node:
def __init__(self, data):
# Initialize a new node with data, previous, and next pointers
self.data = data
self.next = None
self.prev = None
def insert_at_end(head, data):
# Insert a new node at the end of the doubly linked list
new_node = Node(data)
if head is None:
return new_node
current = head
while current.next:
current = current.next
current.next = new_node
new_node.prev = current
return head
def display(head):
# Display the doubly linked list elements
current = head
while current:
# Print current node's data
print(current.data, end=" <-> ")
# Move to the next node
current = current.next
print("None")
# Driver Code
head = None
head = insert_at_end(head, 1)
head = insert_at_end(head, 2)
head = insert_at_end(head, 3)
print("Doubly Linked List after insertion at the end:")
display(head)
OutputDoubly Linked List after insertion at the end:
1 <-> 2 <-> 3 <-> None
Deletion of Doubly Linked List in Python:
Deletion of a node in Doubly Linked List generally involves modifying the next and the previous pointers of nodes. Deletion can be done in 3 ways:
- At the beginning of DLL
- At the end of DLL
- At a given position in DLL
1. Deletion at the beginning:
To delete a node from the beginning of a doubly linked list in Python, you need to follow these steps:
- Check if the list is empty (head is None). If it is empty, there is nothing to delete.
- If the list has only one node, set the head to None to delete the node.
- Otherwise, update the head to point to the next node.
- Set the "previous" pointer of the new head to None.
- Optionally, free the memory allocated to the deleted node.
Below is the implementation of the above idea:
Python
# Python Program for the deletion at the beginning
class Node:
def __init__(self, data):
# Initialize a new node with data, previous, and next pointers
self.data = data
self.next = None
self.prev = None
def delete_at_beginning(head):
# Delete the first node from the beginning of the doubly linked list
if head is None:
print("Doubly linked list is empty")
return None
if head.next is None:
return None
new_head = head.next
new_head.prev = None
del head
return new_head
def traverse(head):
# Traverse the doubly linked list and print its elements
current = head
while current:
# Print current node's data
print(current.data, end=" <-> ")
# Move to the next node
current = current.next
print("None")
def insert_at_beginning(head, data):
# Insert a new node at the beginning of the doubly linked list
new_node = Node(data)
new_node.next = head
if head:
head.prev = new_node
return new_node
# Driver Code
head = None
head = insert_at_beginning(head, 4)
head = insert_at_beginning(head, 3)
head = insert_at_beginning(head, 2)
head = insert_at_beginning(head, 1)
# Delete the first node (node with data 1) from the beginning:
head = delete_at_beginning(head)
# Traverse and print the nodes after deletion:
traverse(head)
Output2 <-> 3 <-> 4 <-> None
2. Deletion at a given position:
To delete a node at a given position in a doubly linked list in Python, you need to follow these steps:
- Check if the list is empty (head is None). If it is empty, there is nothing to delete.
- If the position is less than 0, print an error message as it's an invalid position.
- If the position is 0, delete the node at the beginning of the list.
- Traverse the list to find the node at the given position.
- Update the "next" pointer of the previous node to skip the node to be deleted.
- Update the "previous" pointer of the next node to point to the previous node of the node to be deleted.
- Optionally, free the memory allocated to the deleted node.
Below is the implementation of the above idea:
Python
# Python Program for Deletion of a given node
class Node:
def __init__(self, data):
# Initialize a new node with data, previous, and next pointers
self.data = data
self.next = None
self.prev = None
def delete_at_position(head, position):
# Delete the node at a given position from the doubly linked list
if head is None:
print("Doubly linked list is empty")
return None
if position < 0:
print("Invalid position")
return head
if position == 0:
if head.next:
head.next.prev = None
return head.next
current = head
count = 0
while current and count < position:
current = current.next
count += 1
if current is None:
print("Position out of range")
return head
if current.next:
current.next.prev = current.prev
if current.prev:
current.prev.next = current.next
del current
return head
def traverse(head):
# Traverse the doubly linked list and print its elements
current = head
while current:
# Print current node's data
print(current.data, end=" <-> ")
# Move to the next node
current = current.next
print("None")
def insert_at_beginning(head, data):
# Insert a new node at the beginning of the doubly linked list
new_node = Node(data)
new_node.next = head
if head:
head.prev = new_node
return new_node
# Driver Code
head = None
head = insert_at_beginning(head, 4)
head = insert_at_beginning(head, 3)
head = insert_at_beginning(head, 2)
head = insert_at_beginning(head, 1)
# Delete the node at position 2 (node with data 3):
head = delete_at_position(head, 2)
# Traverse and print the nodes after deletion:
traverse(head)
Output1 <-> 2 <-> 4 <-> None
3. Deletion at the end:
To delete a node at the end of a doubly linked list in Python, you need to follow these steps:
- Check if the list is empty (head is None). If it is empty, there is nothing to delete.
- If the list has only one node, set the head to None to delete the node.
- Traverse the list to find the last node.
- Set the "next" pointer of the second-to-last node to None.
- Optionally, free the memory allocated to the deleted node.
Below is the implementation of the above idea:
Python
# Python Program Deletion at the end
class Node:
def __init__(self, data):
# Initialize a new node with data, previous, and next pointers
self.data = data
self.next = None
self.prev = None
def delete_at_end(head):
# Delete the last node from the end of the doubly linked list
if head is None:
print("Doubly linked list is empty")
return None
if head.next is None:
return None
current = head
while current.next.next:
current = current.next
del_node = current.next
current.next = None
del del_node
return head
def traverse(head):
# Traverse the doubly linked list and print its elements
current = head
while current:
# Print current node's data
print(current.data, end=" <-> ")
# Move to the next node
current = current.next
print("None")
def insert_at_beginning(head, data):
# Insert a new node at the beginning of the doubly linked list
new_node = Node(data)
new_node.next = head
if head:
head.prev = new_node
return new_node
# Driver Code
head = None
head = insert_at_beginning(head, 4)
head = insert_at_beginning(head, 3)
head = insert_at_beginning(head, 2)
head = insert_at_beginning(head, 1)
# Delete the last node (node with data 4) from the end:
head = delete_at_end(head)
# Traverse and print the nodes after deletion:
traverse(head)
Output1 <-> 2 <-> 3 <-> None
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem