Introduction and Insertion
In this lesson, you will be introduced to circular linked lists and you will learn how to insert elements into a circular linked list.
We'll cover the following...
Introduction
First of all, let’s talk about what a circular linked list is. It is very similar to a singly linked list except for the fact that the next of the tail node is the head node instead of null.
Below is an illustration to help you visualize a circular linked list:
You can see from the illustration above that the circular linked list contains the same kind of nodes as a singly linked list. As the name circular suggests, the tail node points to the head of the linked list instead of pointing to null which makes the linked list circular. Now let’s implement it in Python:
class Node:def __init__(self, data):self.data = dataself.next = Noneclass CircularLinkedList:def __init__(self):self.head = Nonedef prepend(self, data):passdef append(self, data):passdef print_list(self):pass
The Node class is the same as before. Also, the constructor of CircularLinkedList class is identical to the constructor of the LinkedList class. However, the methods prepend
, append
, and print_list
will be different this time.