Delete Node in Linked List in Python (Example)
In this tutorial, you’ll learn how to delete a node in a linked list using the Python programming language.
The table of contents is structured as follows:
Let’s dive into it!
Introduction to Deleting a Node in a Linked List
A linked list is a data structure where each element, known as a node, contains a value and a reference to the next node in the sequence. Deleting a node in a linked list involves removing a specific node from the list and reorganizing the links to maintain the integrity of the list.
Deleting a Node in a Linked List
To delete a node in a linked list in Python, you can follow these steps:
Step 1: Traverse the linked list to find the node to be deleted.
Step 2: Update the links to bypass the node to be deleted.
Step 3: Free the memory occupied by the deleted node (In Python garbage collection is done automatically)
Here’s an example of deleting a node in a linked list:
class Node: def __init__(self, value): self.value = value self.next = None def delete_node(head, value): if head is None: return head if head.value == value: return head.next current = head while current.next is not None: if current.next.value == value: current.next = current.next.next break current = current.next return head
In the above code, we define a class Node
to represent a node in the linked list. The delete_node
function takes the head of the linked list and the value of the node to be deleted as parameters. It traverses the linked list and updates the links to bypass the node with the specified value. Finally, it returns the updated head of the linked list.
If the entered value isn’t found in the linked list, no exception is raised, and the linked list is returned without any alterations. Also, the implementation is slightly different if our linked list is a doubly linked list or a circular linked list, which you can check out in our tutorials about their implementations.
Let’s see it in an example!
Example
In this example, first, I will create a linked list, then I will remove a node from it.
# Create a linked list head = Node(10) node2 = Node(20) node3 = Node(30) head.next = node2 node2.next = node3 # Delete a node with value 20 head = delete_node(head, 20) # Print the updated linked list current = head while current is not None: print(current.value) current = current.next # 10 # 30
Above, we create a linked list with three nodes and delete the node with the value 20. The resulting linked list is then printed.
Video, Further Resources & Summary
Do you need more explanations on deleting a node in a linked list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Check if List of Lists is Empty in Python
- Convert List from Character String to Integer in Python
- List in Python (Beginner & Advanced)
- Check If Key Exists in List of Dictionaries in Python
- Access Dictionary within List in Python
- Python Programming Tutorials
This post has shown how to delete a node in a linked list in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.
Statistics Globe Newsletter