Last Updated: 20 January, 2025 5 Mins
Hey, In this article we are going to learn all about Circular Linked List in detail and their unique structure, which allows for the last node to point back to the first node of the list, creating a continuous loop. Next we will see its types, operations, complexity, implementation, and more.
A circular linked list is another type of linked list where the last node points to the first node of the list, creating a circular structure. Unlike a normal linked list, a circular linked list does not have a NULL pointer at the end.
As with other linked lists, circular linked lists can also grow or shrink dynamically as elements are added or removed.
A circular linked list can be created using the singly linked lists and doubly linked lists so there are basically two types of circular linked list:
1. Circular Singly Linked List
A singly linked list's last node points to its first node, creating a circular structure. There will be no NULL reference, and it will traverse only in the forward direction.
2. Circular Doubly Linked List
In a doubly linked list, each node has two pointers (prev and next) and one data field. To create the Circular Doubly Linked List, we will store the first node address in the last node next pointer and the last node address in the first node prev pointer, forming a circle structure. The list will not include any NULL references and will traverse both forwards and backwards directions.
We can perform the following operations on the Circular Linked List as given below.
Operations | Time Complexity | Space Complexity |
---|---|---|
Insertion | O(1) to O(n) | O(1) |
Deletion | O(1) to O(n) | O(1) |
Traversal | O(n) | O(1) |
Search | O(n) | O(1) |
Access | O(n) | O(1) |
Now, let's understand the implementation of circular linked list program in Java.
Class: Node
Class: CircularLinkedList
Class: CircularLinkedListOutput
Output
Linked List Elements: 20 40 60 80
Given key 60 deleted successfully.
Key not found in the list
Linked List Elements: 20 40 80
There are the following schenaries where circular linked list can be used.
That's all, guys. I hope this article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com