You are currently viewing Singly Linked List in Python with Examples

Let’s discuss how to implement a Singly Linked List using the dstructure module in python. A Linked List is a one-dimensional data structure containing nodes with a data field and a ‘next’ field, which points to the next node in a line of nodes.

Advertisements

Related: Doubly Linked List Explained with Examples

After going through this Python guide, you will know how to create, insert, delete, display the data present in the Linked List.

  1. Create a Single Linked List using SLL()
  2. Insert elements into Singly Linked List using insert()
  3. Delete the first node from the Linked List using delete_f() method.
  4. Delete the last node from the Linked List using delete_l() method.
  5. Delete a particular node from the Linked List using delete() method.
  6. Return all nodes of the Linked List using the getnodes() method.
  7. Print all nodes of the Linked List using the print() function.

It is important to install dstructure module before importing it. Before importing, make sure you have installed it by using the below command.


# Install dstructure
pip install dstructure

1. Quick Examples of using Singly Linked List

Following are quick examples of operations on a singly linked list.


from dstructure.SLL import SLL

# Create Singly Linkedlist.
single_ll=SLL() 

# Insert 5 elements using insert()
single_ll.insert('China') 
single_ll.insert('Japan')
single_ll.insert('India') 
single_ll.insert('Russia')
single_ll.insert('Mexico') 

# Print the linkedlist 
single_ll.print()

# Delete first node using delete_f()
single_ll.delete_f()
single_ll.print()

# Delete last node using delete_l()
single_ll.delete_l()
single_ll.print()

# Delete particular node using delete()
single_ll.delete('India')
single_ll.print()

# Return all nodes in a list using getnodes()
single_ll.getnodes()

2. Create a Singly Linked List using SLL()

After importing the SLL module from the dstructure package, Single Linked List can be created using SLL(). By using SLL() creates an empty singly linked list.


# Import Singly Linked List
from dstructure.SLL import SLL

# Create Singly Linkedlist.
single_ll=SLL() 

3. Insert Element into Python Singly Linked List

The insert() method is used to insert a single item into Single Linked List. It takes an element to be inserted as a parameter.

If we print the Linked List using the print() function, all the elements from the Linked List are displayed separated by “->”.

Let’s insert 3 countries (string type) one after the other to the above created single linked list.


# Insert 3 elements using insert()
single_ll.insert('China') 
single_ll.insert('Japan')
single_ll.insert('India') 

single_ll.print()

# Output:
# China -> Japan -> India

Three countries are inserted into the single linked list.

4. Delete First Node from Singly Linked List

The first node from Python singly linked list is deleted using delete_f() method, this method returns True after successfully deleting a node. If the Single linked list is empty, False is returned. It takes no parameter.

Example 1: Let’s consider the above Single linked list and delete the first node.


# Delete first node using delete_f()
single_ll.delete_f()

# Output:
# True

Since the Single linked list is not empty, the First node – ‘China’ is deleted and returns True.

Example 2: Let’s display the Linked list after deleting the first node.


single_ll.print()

# Output:
# Japan -> India

5. Delete Last Node from Linked List

The last node from singly linked list is deleted using delete_l() method. This returns true after deleting node from the end. If the doubly linked list is empty, False is returned. It takes no parameter.

Example 1: Let’s consider the Single linkedlist with no elements and try to delete the last node.


from dstructure.SLL import SLL
single_ll=SLL() 

# Delete last node using delete_l()
single_ll.delete_l()

# Output:
# False

Since the Single linked list is empty, False is returned.

Example 2: Let’s consider the Single linked list with some elements and display it after deleting the last node.


from dstructure.SLL import SLL

single_ll=SLL() 
single_ll.insert('China') 
single_ll.insert('Japan')
single_ll.insert('India') 

# Delete last node using delete_l()
single_ll.delete_l()

single_ll.print()

# Output:
# China -> Japan

6. Delete a Particular Node from Linked List

If you want to delete a particular element from the Singly Linked list, you can use delete() method. It will take the element as a parameter that you wanted to delete.

Let’s consider the Single linked list with 3 elements and delete ‘Japan’.


# Import
from dstructure.SLL import SLL

# Insert
single_ll=SLL() 
single_ll.insert('China') 
single_ll.insert('Japan')
single_ll.insert('India') 

# Delete particular node using delete()
single_ll.delete('Japan')
single_ll.print()

# Output:
# China -> India

We can see ‘Japan’ is deleted from the Single linked list.

7. Get All Nodes from Singly Linked List in Pyhton

Python getnodes() method will return all the nodes from the singly linked list. Let’s consider the singly linked list with 3 elements and display them.


from dstructure.SLL import SLL

single_ll=SLL() 
single_ll.insert('China') 
single_ll.insert('Japan')
single_ll.insert('India')

# Return all nodes in a list
single_ll.getnodes()

# Output:
# ['China', 'Japan', 'India']

8. Conclusion

First, we have seen how to create Python Singly LinkedList using SLL() and then we used the insert() method to put elements into it. The first node of the linked list is deleted using the delete_f() method and the last node of the linked list is deleted using the delete_l() method. If you want to delete a particular node, you can use the delete() method. getnodes() method is used to return all the nodes from the Linkedlist in a list.