Linked Lists in Python. Linked List Data Structures in Python | by Sadrach Pierre, Ph.D. | Towards Data Science

Data structures provide ways of organizing data such that we can perform operations on the data efficiently. One important data structure is the linked list. A linked list is a linear collection of nodes, where each node contains a data value and a reference to the next node in the list. In this post, we will discuss how to implement linked lists in python.
Let’s get started!
We begin by creating two classes, a ‘Node’ class and a ‘LinkedList’ class. For the Node class we have:
class Node:     def __init__(self, data):         self.data = data         self.next = None
Here we assign data values and initialize the next node as null. And for the ‘LinkedList’ class we initialize the head node as null:
class LinkedList:     def __init__(self):          self.head = None
The linked list class, which will contain a reference of the Node class type, is used to initialize the linked list object. We can now write an instance of a linked list and print the result:
if __name__=='__main__':    linkedlist = LinkedList()    print(linked list)
We see we have a linked list object at the specified address. Next we can define a few nodes. Let’s work towards a real life example. Music players typically have doubly linked lists containing songs linked to previous and next songs. For simplicity, let’s consider singly linked list of songs, where each song will be only linked to the next song.
Let’s create a linked list of the songs on first half of the album Dark Side of the Moon, by Pink Floyd. To start, we define the values of each node:
if __name__=='__main__':    linkedlist = LinkedList()    linkedlist.head = Node('Speak to Me')     second = Node('Breathe')     third = Node('On the Run')     fourth = Node('Time')     fifth  = Node('The Great Gig in the Sky')
Now we need to link all of the nodes. First we define the ‘next’ value of the head node:
linkedlist.head.next = second
Then the ‘next’ value of the ‘second’ node:
second.next = third
And so forth:
third.next = fourthfourth.next = fifth
Now that our linked list is defined, we can traverse the linked list. Let’s define a method, in the ‘LinkedList’ class, that will allow us to do so:
def printLinkedList(self):         value = self.head         while (value):             print(value.data)             value = value.next
We can now traverse the list :
if __name__=='__main__':    #initialize linked list object    linkedlist = LinkedList()    print(linkedlist)        #assign values to nodes     linkedlist.head = Node('Speak to Me')     second = Node('Breathe')     third = Node('On the Run')     fourth = Node('Time')     fifth  = Node('The Great Gig in the Sky')         #link nodes    linkedlist.head.next = second    second.next = third    third.next = fourth    fourth.next = fifth            linkedlist.printLinkedList()
To summarize, in this post we discussed how to implement a singly linked list in python. We developed a singly linked list for a music player, where each song is linked to the next song in the list. In practice, songs in a music player are linked to previous and next songs (doubly linked list implementation). I encourage you to modify the code in this post to implement a doubly linked list for the songs we used. The code in this post is available on GitHub. Thank you for reading!