print_list is a class method, so it will take self as an argument and print out the entries of a linked list. We will start from the head pointer and print out the data component of the node and then move to the next node. We’ll keep a check on the next node to make sure it is not None. If it’s not, we move to the next node. This way, we keep printing out data until we’ve hit the null terminating component of the linked list. Let’s implement this in Python!
There you go! We initialize cur_node equal to the head of the linked list. Then we use a while loop which keeps running and printing the data if cur_node is not equal to None.
In the code above, we append four elements to the linked list. You can see this for yourself in the output.
Now we’ll move on to another method of inserting elements in a linked list.
To verify the append method, we will need to print the circular linked list. Let’s see how we would do this. Check out the code below: