Python: Linked Lists

 Hello and Welcome to Blog #7, Python: Linked Lists. This blog will introduce the concept of linked lists. 


Linked lists are a type of data structure. However, there are some differences from arrays and lists. In a linked list, every element is linked to another sequentially. 

In an array, like [1,2,3,4,5,6], there are six elements. You can iterate through the array, or access a specific element at its index (see Python: Arrays). With a linked list, there are no 'indexes'. Instead, the structure is represented as such:

1 -> 2 -> 3-> 4-> 5-> 6

In this specific linked list, the head, or the starting element is the beginning of the linked list. In a linked list, the elements are called Nodes. The head Node is then linked to the next Node, two, which is then linked to three. 

The Nodes are all separate data holders. In other words, they are not literally numbers. Instead, they have a 'val' attribute, which holds the value of that Node, which is accessed by saying, Node.val.  To go from one Node to the next, you use, Node.next. This just goes to the Node that this one is linked to.


These are the basics of Linked Lists in Python. The described attributes must be defined separately, and this is just the basics of linked lists. There are many more complicated ones, and Nodes can have more attributes. There could be doubly linked lists, where each Node is linked to both the previous and the next Node. These lists can be traversed both forward or backward, and could even have multiple levels of Nodes. Also, the defined attributes do not necessarily need to be named 'val' or 'next.' Their names do not change anything, only the way you define them and use them. 


Thank you for reading Python: Linked Lists. Please leave any comments and questions in the comments section!

Comments