Python is considered as an easiest language of programming. Indeed it is easiest, in terms that it has far less rules and syntax than any other language so far, and the main beauty of python is that it is object oriented.
Being easier does not mean you don't have to understand concepts. So, that being said let's move to our our topic today i.e. python linked list and its reversing.
Some of the learners that are new to python may have hard time understanding reversing of linked list. It is only so, when you did not understand python linked list correctly.
What is Linked List?
List is basically a group of two or more than two items. And, as the name "Linked List" suggest, some numbers of list are somehow linked together. To understand a Linked List completely, we have to understand how these list are linked to each other.
In linked list, each list (a single list in many linked list) can only contain two items.
The first list will linked to second list in a way that the second list will be the second item of the first list. Similarly, the third list will be the second item of the second list and so on until in a list, the second item is None. (Note that this line is just to understand only.)
You can refer to the picture above at the top. The picture shows the first list containing a data and a next list which itself contains a data and a list and so on until the list is None.
Python Node class for Linked List:
Let's first define a class that will create lists. Considering each list as a Node because each list will link all lists next to them playing a role as a node for all the nodes after it.
This simple Node class can create Linked List in Python. You can understand it in the picture given below.
But this does not make our job of appending data any easier.
Python Linked List class:
So, we will create another class named LinkedList that will have a method to facilitate appending using Node class created above, and that will handle our Linked List.
In above code, while a appending a data in our list object, we are checking if we have any head node to initiate with. If our head node is none, we are assigning `self.head` our first node and appending the data (or item) in it. If it is not none, we are tracking the last node whose next node is none, and then we are assigning a node and appending the data to it.
Like `append()` method, you can also add methods to print list data, get the length of list and to find an item at specific position.
Reversing a Linked List:
While reversing a linked list if the list is none we return none. But if the list is not none,
1. we take head node as current node
2. we take none as previous node
3. On each loop, we track the nodes after current node and assign it to next node
4. Now we can remove the next node from the current node and set the previous node as the next node of current node.
5. Now for the next loop, we set current node as previous node and next node as current node.
At the end of each loop, we will have previous node and current nodes as follows:
This can be simply understood by taking a simple example and and executing each line of code in while loop manually with pen and paper. If you understood how the lists are linked then understanding its reversing is a piece of cake.
You can also check out my notebook on Python Linked List here on Jovian.
0 Comments
Your comment will be moderated.