Member-only story
Python does not have linked lists built into its programming language, as other languages such as Java do, so it is necessary to write code to create a linked list. In a recent post I discussed linked list, and that post can be found here:- https://medium.com/@tracyrenee61/interview-question-create-a-linked-list-in-python-8ab23471db09
While Python does not have linked lists built in to it’s language, it does have lists. In addition, in Python, there is no distinction between a list and array, which further simplifies that language to some degree.
Sometimes linked lists are preferred over lists, however, because they are more memory efficient than arrays. Unlike lists, the size of the linked list is not predefined, which allows the linked list to increase or decrease in size as the program runs. Because Python does not have linked lists inbuilt into the programming language, therefore, it will sometimes be necessary to convert a list to a linked list.
The code below will convert a list to a linked list by:-
- Define the class node, which will create a node that will be used in the linked list.
- Define the function insert, which will insert a node into a list.
- Define the function, display, which will display the linked list.