- Singly Linked List
- How to implement a singly linked list in Python?
- Step 1: Create a Node class
- Step 2: Create a linked list class that will manage nodes by adding, deleting, and traversing through the nodes.
- Step 3: Define the methods in the LinkedList that will add a node at the end, insert a node in between, delete a node from the LinkedList and traverse node in the linked list.
- Python program to implement singly list
- Output
- Doubly Linked List
- Doubly Linked list Implementation in Python
- Step 1: Create a Node class
- Step 2: Define the linked list class
- Step 3: Define the operations of a doubly-linked list as LinkedList Methods
- Traverse method
- Python Doubly linked list program with append, insert, delete and traverse methods
- Output
- Conclusion
A linked list is one of the basic data structures present in computer science. To represent a linked list we create a sequence of nodes or a chain of nodes. In the sequence of nodes, the first node is called the head of the Linked List and the last node is known as the tail. In the linked list every node has a pointer to the next node, which means each node contains a piece of extra information next representing the next node connected to it.
Singly Linked List[/caption] The above image is an example of a singly linked list, where each node is pointing to its next node, except for the Tail node. Apart from a singly linked list, there is also a data structure which is known as a doubly-linked list.
Doubly Linked List[/caption] In this doubly linked list, each node contains two pointers, one pointer to the next node and one pointer to the previous node. There are many cases where we use the linked list to solve real-world problems. Linked lists provide an alternative to the standard list or arrays present in Python.
- Linked list allow us to add and remove elements with linear time complexity.
- Managing items in a linked list are also very easy.
- In linked list insertion item in the middle of the list is cheap as compared to the list and array data structure.
Note: The linked list is an alternative to the static arrays, whereas in Python the list data structure is more a dynamic data structure, which is as efficient as a linked list. In this tutorial, we will learn the concept of the Linked List data structure and learn how to implement a custom Linked List in Python using class and objects.
Singly Linked List
A singly linked list is a sequential data structure, made of nodes. A node is a single block in the linked list structure that contains some data. We can also define a linked list as a sequential chain of nodes. Where the first node is known as the Head and the last node Tail .
In the singly linked list, each node contains a pointer to the next node, which connects one node to the next node in the linked list. Only the tail node does not point to any next node.
How to implement a singly linked list in Python?
Here are the steps to define or implement a singly linked list in Python
Step 1: Create a Node class
For the singly linked list, the node class can contain any type of data, but it must have a next pointer that can point to the next node. The initial value of the next node should be None because it is always expected to be the last node in the linked list.
The Node class can accept a data argument.
Step 2: Create a linked list class that will manage nodes by adding, deleting, and traversing through the nodes.
Whenever we create a linked list, it should have two information when initializing its object, the head node, and tail node information. But when we define a linked list, without any nodes in it the value of Head and Tail would be None .
The
self.length
property is just for some extra information about the linked list. The
self.length
property defines the total length of the linkedlist.
Step 3: Define the methods in the LinkedList that will add a node at the end, insert a node in between, delete a node from the LinkedList and traverse node in the linked list.
Let’s start with the method that will add the node to the linked list.
Add Node in the Linked List.
In the above example, we have defined the append method in the LinkedList class to add a new node to the Linked List. In the first, if condition
if not self.head:
the statement is checking if the LinkedList already has some node or it's empty. If the LinkedList is empty then the value of
self.head
and
self.tail
would be None. And the node that is added to the linked list would be the first node and it will be the head and tail node for the LinkedList. In the else statement, the code is instructing that if the LinkedList already has one or more than one node, then create the new node, point the current tail node next pointer to the new node(because it is adding at the last), and set the value of tail to the new node because the LinkedList now have a new tail node.
Insert a Node in the Linked List
In the above section, we see how can we add a node at the end of the Linked List, now let’s write
insert(pos, data)
,
that can insert the new node in between the linked list.
Delete a Node in the Linked List
To delete a node from the linked list, we will point the previous node of the Node to the next node of the node.
Traverse through the linked list
To traverse through the linked list means to go through every linked list node and display them.
Python program to implement singly list
Put all the code together and run
Output
Doubly Linked List
The doubly linked list is similar to the singly linked list, the only difference is, the doubly linked list node contains two pointers
prev
and the
next
. The prev pointer points to the previous node and the next pointer points to the next node in the linked list. In the case of a doubly-linked list the head node’s
prev
points to None and the next points to the next node and the tail node’s prev points to the second last node, and the next points to None.
Doubly Linked list Implementation in Python
Step 1: Create a Node class
Both singly and doubly linked lists are a sequential chain of nodes. And similar to the singly linked list we have to create a Node class for the doubly linked list.
The node of the doubly linked list contains two pointers self.next and self.prev, that points to the next and previous node.
Step 2: Define the linked list class
After defining the Node class let’s define the LinkedList class for the doubly linked list, which will contain methods and properties to manage the linked list.
Step 3: Define the operations of a doubly-linked list as LinkedList Methods
Let’s start with append method that will add the node at the end of the linked list.
In the append method, we set 2 conditions in the.
- The first condition is set for the linked list if there is no node in the linked list.
- And the second condition is set if there is one or more than one node in the linked list. And we want to add the new node at the end of the list.
Insert Method
In the insert method, we will define the insert new node in the doubly linked list. To insert a new node we need set select a position where the node is needed to insert.
Delete Method
We can also delete the node from a doubly linked list. To delete a node we just need to manipulate the targeted node's previous and next nodes.
Traverse method
As the Doubly linked list has two pointers next and prev, we can traverse a doubly linked list from head to tail and tail to head.
Python Doubly linked list program with append, insert, delete and traverse methods
Now put all the code together and execute.
Output
Conclusion
In this Python tutorial, we learned what is singly and doubly linked list in Python and how to implement them. In a singly linked list, every node has a next pointer variable that points to the next node, which makes the singly linked list a unidirectional sequential data structure. On the other hand, the doubly linked list’s nodes have two pointer variables next and prev( previous), which point to the next and previous nodes.
People are also reading:
- Python Class Method with examples
- Python Class Variables
- Replace Item in Python List
- Python List Methods
- Python List Files in a Director y
- Remove Duplicates from a Python List
- Python Instance Method
- Python Instance Variables
- Timestamp in Python
- Python Regex Replace Pattern in a string using re.sub()
Leave a Comment on this Post